我知道有很多问题和答案,但遗憾的是我认为我的情况可能是独一无二的?出于某种原因,时间变化似乎使得这一天的计算时间比计算的时间少一天。
这是我正在使用的PHP,它工作得很好,直到它开始重叠开始和结束日期之前的日光节约和夏令时之后(FYI,这是最近的问题,因为白天节约时间从本周末开始!):
//$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)));
再说一次,要明确的是,如果我没有重叠夏令时的变化,上述工作就会起作用......
供参考:
How to find the dates between two specified date?
Actual days between two unix timestamps in PHP
Finding days between 2 unix timestamps in php
而且,我现在仍然使用PHP 5.2。
修改/更新:
我在这个链接上找到了以下内容:
How to find the dates between two specified date?
和
//$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的交叉。有人看到任何问题吗?
答案 0 :(得分:2)
你是在运行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');