不,这不是日期之间的标准+86400秒。
$start_time = strtotime("2012-01-15 23:59");
$end_time = strtotime("2012-01-16 00:05");
$ daysInBetweenTimestamps =?
这是我目前面临的问题,因为时间戳可能在5分钟到5小时之间,例如,使用标准+86400查看是否超过一天是行不通的,并且由于数量过多我正在建立索引的过程,我想看看是否有更有效的方法来检查新的一天是否开始,而不是在第二层上执行date(“ 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开始===
0 days
Single day so log files are NOT split
我是否只是在差异上缺少某些东西?
===示例1结束===
===示例3开始===
Number of days:1
Multiple days so log files are split
===示例3结束===
$days = unixtojd($t1) - unixtojd($t2);
或者如果您不在UTC中...
$days = unixtojd($t1 - $tz) - unixtojd($t2 - $tz);
其中$tz
是您的时区偏移量,以秒为单位。
使用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
将时间戳取整到最接近的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。
<?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";
?>
$ current_time = time(); //或您的日期$ your_date = strtotime(“ 2020-04-27”); //这是我的提交时间。
$ date_diff = $ current_time-$ your_date;$ num_of_days = round($ date_diff /(60 * 60 * 24));
//您的答案在$ num_of_days变量中