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

时间:2012-01-27 15:19:50

标签: php datetime timestamp days

这不是日期之间的标准+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)."<br />";
echo "Absolute End: ".date("Y-m-d H:i:s",$end)."<br />";
echo "Interval Used: $interval(seconds) OR ".($interval / 60)."(minutes)<br />";
$numberOfIntervals = ceil(($end - $start) / $interval);
echo "Number of intervals:$numberOfIntervals<br /><br />";
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)."<br />";
        echo "Interval End DateTime: ".date("Y-m-d H:i:s",$curEnd)."<br />";
/* EXAMPLE PHP5.3 DateTime START - NOT WORKING */
        $startDiff = new DateTime("@$curStart");
        $endDiff = new DateTime("@$curEnd");
        $diff = $startDiff->diff($endDiff);
        echo $diff->format("%a") . " days<br />";
        if ($diff->format("%a") > 0){
/* EXAMPLE PHP5.3 DateTime END */
/* EXAMPLE Julian START - WORKS */
            $days = unixtojd($curEnd) - unixtojd($curStart);
            echo "Number of days:$days<br />";
            if ($days > 0){
/* EXAMPLE Julian END */
                // Multiple days so the log files are split
                echo "Multiple days so log files are split<br />";
            }else{
                echo "Single day so log files are NOT split<br />";
            }
        }
    }

输出如下:

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

===示例1 START ===

0 days
Single day so log files are NOT split

我刚刚错过了差异吗?

===示例1结束===

===示例3 START ===

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

===示例3结束===

5 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

使用Julian Day

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

或者如果你不在UTC ......

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

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

答案 2 :(得分:0)

将时间戳向下舍入到最接近的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。

答案 3 :(得分: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";
?>

答案 4 :(得分:0)

$current_time = time(); // or your date as well
$your_date = strtotime("2020-04-27"); //this is my submit time.

$date_diff = $current_time - $your_date;
$num_of_days = round($date_diff / (60 * 60 * 24));

//you have your answer in $num_of_days variable