PHP中两个unix时间戳之间的实际日期 [英] Actual days between two unix timestamps in PHP

查看:129
本文介绍了PHP中两个unix时间戳之间的实际日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不,这不是日期之间的+86400秒。

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

$ daysInBetweenTimestamps =?



我目前面临的问题是时间戳可能在5分钟到5小时的时间段之间,例如使用标准+86400来查看是否超过一天将不起作用,并且由于大量的索引,我我想看看是否有更有效的方式来检查新的一天是否已经开始,而不是在第二级做一个日期(d)> $ prevDay。



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

  echo绝对开始:.date(Ymd H:i:s,$ start)。< br />; 
echoAbsolute End:.date(Y-m-d H:i:s,$ end)< br />;
echoInterval Used:$ interval(seconds)OR。($ interval / 60)。(minutes)< br />;
$ numberOfIntervals = ceil(($ end - $ start)/ $ interval);
echo间隔数:$ numberOfIntervals< br />< br />;
if($ numberOfIntervals> 0){
for($ i = 0; $ i <$ numberOfIntervals; $ i ++){
$ curStart = $ start +($ interval * $一世);
$ curEnd = $ curStart + $ interval;
if($ curEnd> $ end){$ curEnd = $ end;}
echoInterval Start DateTime:.date(Ymd H:i:s,$ curStart)< ; br />;
echoInterval End DateTime:.date(Y-m-d H:i:s,$ curEnd)< br />;
/ *示例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){
/ *示例PHP5.3 DateTime END * /
/ *示例Julian START - WORKS * /
$ days = unixtojd($ curEnd) - unixtojd($ curStart);
echo天数:$ days< br />;
如果($ days> 0){
/ *示例Julian END * /
//多天,所以日志文件被拆分
echo多天,所以日志文件是split< br />;
} else {
echo单日,所以日志文件不分裂< br />;
}
}
}

输出如下所示: / p>

 绝对开始:2012-01-25 23:59:00 
绝对结束:2012-01-26 00: 02:00
间隔时间:180(秒)或3(分钟)
间隔数:1
间隔开始日期时间:2012-01-25 23:59:00
间隔结束日期时间:2012-01-26 00:02:00

===示例1 START ===

  0天
单日,所以日志文件不分裂

我只是错过了一些差异?



===示例1 END ===



===示例3开始===

 天数:1 
多天,所以日志文件被拆分

===示例3 END ===

解决方案

使用

或如果您不在UTC ...


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

其中 $ tz 是您的时区偏移量秒。


No this is not the standard +86400 seconds between dates.

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

$daysInBetweenTimestamps = ?

That is the problem I'm currently facing as the timestamps may range in between a 5 minute to 5 hour time span for instance, using standard +86400 to see if it's more than a day would not work, and due to massive amount of indexing that I'm doing I would like to see if there is a more efficient way to check if a new day has started instead of doing a date("d") > $prevDay on the second level.

Updating with the test for the first example:

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 />";
            }
        }
    }

Output looks as follows:

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

Am I just missing something on the diff?

=== EXAMPLE 1 END ===

=== EXAMPLE 3 START ===

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

=== EXAMPLE 3 END ===

解决方案

Use the Julian Day

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

or if you are not in UTC...

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

where $tz is your timezone offset in seconds.

这篇关于PHP中两个unix时间戳之间的实际日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆