首页 文章

PHP中两个unix时间戳之间的实际天数

提问于
浏览
1

不,这不是日期之间的标准86400秒 .

$start_time = strtotime("2012-01-15 23:59");
$end_time = strtotime("2012-01-16 00:05");

$ daysInBetweenTimestamps =?

这就是我目前面临的问题,因为时间戳可能介于5分钟到5小时之间,例如,使用标准86400来查看它是否超过一天不起作用,并且由于大量的索引我正在做我想看看是否有更有效的方法来检查新的一天是否已经开始,而不是在第二级进行日期(“d”)> $ prevDay .

使用第一个示例的测试进行更新:

echo "Absolute Start: ".date("Y-m-d H:i:s",$start)."
"; echo "Absolute End: ".date("Y-m-d H:i:s",$end)."
"; echo "Interval Used: $interval(seconds) OR ".($interval / 60)."(minutes)
"; $numberOfIntervals = ceil(($end - $start) / $interval); echo "Number of intervals:$numberOfIntervals

"; if ($numberOfIntervals > 0){ for ($i = 0; $i < $numberOfIntervals; $i++){ $curStart = $start + ($interval * $i); $curEnd = $curStart + $interval; if ($curEnd > $end){$curEnd = $end;} echo "Interval Start DateTime: ".date("Y-m-d H:i:s",$curStart)."
"; echo "Interval End DateTime: ".date("Y-m-d H:i:s",$curEnd)."
"; /* EXAMPLE PHP5.3 DateTime START - NOT WORKING */ $startDiff = new DateTime("@$curStart"); $endDiff = new DateTime("@$curEnd"); $diff = $startDiff->diff($endDiff); echo $diff->format("%a") . " days
"; if ($diff->format("%a") > 0){ /* EXAMPLE PHP5.3 DateTime END */ /* EXAMPLE Julian START - WORKS */ $days = unixtojd($curEnd) - unixtojd($curStart); echo "Number of days:$days
"; if ($days > 0){ /* EXAMPLE Julian END */ // Multiple days so the log files are split echo "Multiple days so log files are split
"; }else{ echo "Single day so log files are NOT split
"; } } }

输出如下:

Absolute Start: 2012-01-25 23:59:00
Absolute End: 2012-01-26 00:02:00
Interval Used: 180(seconds) OR 3(minutes)
Number of intervals:1
Interval Start DateTime: 2012-01-25 23:59:00
Interval End DateTime: 2012-01-26 00:02:00

=== EXAMPLE 1 START ===

0 days
Single day so log files are NOT split

我刚刚错过了一些差异吗?

=== EXAMPLE 1 END ===

=== EXAMPLE 3 START ===

Number of days:1
Multiple days so log files are split

=== EXAMPLE 3 END ===

4 回答

  • 0

    使用Julian Day

    $days = unixtojd($t1) - unixtojd($t2);
    

    或者如果你不在UTC ...

    $days = unixtojd($t1 - $tz) - unixtojd($t2 - $tz);
    

    其中 $tz 是您的时区偏移量,以秒为单位 .

  • 2

    使用php5.3的DateInterval类:

    $now = new DateTime();
    $then = new DateTime("@123456789"); //this is your timestamp
    
    $diff = $now->diff($then);
    
    echo "then: " . $then->format("Y-m-d") . "\n";
    echo $diff->format("%a") . " days\n";
    

    输出:

    then: 1973-11-29
    13937 days
    
  • 2

    将时间戳向下舍入到最接近的86400秒,取差值,然后除以86400:

    $start_time = strtotime("2012-01-15 23:59");
    $end_time = strtotime("2012-01-16 00:05");
    
    $start_time -= $start_time % 86400;
    $end_time -= $end_time % 86400;
    
    $days = ($end_time - $start_time) / 86400;
    

    向下舍入使得当天午夜的每个时间戳都显示,并且除法给出了自纪元以来的天数 . 但这只适用于UTC .

  • 0
    <?php
    $start_time = strtotime("2012-01-15 23:59");
    $end_time = strtotime("2012-01-16 00:05");
    
    $time_zone = 19800; # My time zone: UTC+0530 hours = UTC+19800 seconds
    
    $days = intval(($end_time + $time_zone) / 86400) -
            intval(($start_time + $time_zone) / 86400);
    
    echo "$days\n";
    ?>
    

相关问题