查找两个unix时间戳之间的天数的问题


Problems with finding the days between two unix timestamps

我知道已经有很多问题和答案了,但不幸的是,我认为我的情况可能是独一无二的?由于某种原因,时间的变化似乎使这一天的计算比它应该计算的少了一天。

这是我使用的PHP,它工作得很好,直到它开始重叠开始和结束日期,分别是夏令时之前和夏令时之后(供参考,这是最近的问题,因为夏令时从这个周末开始!):

//$lastDate and $firstDate are 2 unix timestamps with valid month, day, and year values.
//The times are irrelevant at this point, they are only meant to represent a day.
//I start by making sure these have the same time values.
$lastDate = mktime(23, 59, 59, date("m", $lastDate), date("j", $lastDate), date("Y", $lastDate));
$firstDate = mktime(23, 59, 59, date("m", $firstDate), date("j", $firstDate), date("Y", $firstDate));
//Then calculate the total number of days in between.
$totalDays = abs(floor(($firstDate - $lastDate)/(60*60*24)));

因此,再次明确,如果我不重叠夏令时更改,上述工作有效…

参考:

如何查找两个指定日期之间的日期?

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

在php中查找两个unix时间戳之间的天数

而且,我现在还在PHP 5.2上。

编辑/更新:
我在这个链接中发现了以下内容:

如何查找两个指定日期之间的日期?

如何在php中计算两个unix时间戳之间的间隔而不除以86400 (60*60*24)

//$lastDate and $firstDate are 2 unix timestamps with valid month, day, and year values.
//The times are irrelevant at this point, they are only meant to represent a day.
//I start by making sure these have the same time values.
$lastDate = mktime(10, 00, 00, date("m", $lastDate), date("j", $lastDate), date("Y", $lastDate));
$firstDate = mktime(10, 00, 00, date("m", $firstDate), date("j", $firstDate), date("Y", $firstDate));
//Then calculate the total number of days in between. 
$totalDays = floor($firstDate / 86400) - floor($lastDate/ 86400);

现在它似乎可以用于DST的交叉。有人觉得有什么问题吗?

是否使用PHP 5.3+?DateInterval很好地解决了这个问题。
http://www.php.net/manual/en/datetime.diff.php

<?php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');