PHP日期/时间问题

时间:2011-03-09 22:33:30

标签: php date time strtotime

所以我试图在以下日期的天数中获得差异。对于其中一个它可以工作,但对于另一个,这个数字是关闭的。它应该是3。

我可以使用ceil函数,但我仍然不知道为什么以下不正确。

$checkOut = strtotime('2011-02-16');
$checkIn = strtotime('2011-02-11');
echo ($checkOut - $checkIn) / (60 * 60 * 24); // => 5
echo floor(($checkOut - $checkIn)/(60*60*24)); // => 5

$checkOut = strtotime('2011-03-14');
$checkIn = strtotime('2011-03-11');
echo ($checkOut - $checkIn) / (60 * 60 * 24); // => 2.958333333
echo floor(($checkOut - $checkIn)/(60*60*24)); // => 2

3 个答案:

答案 0 :(得分:1)

为什么不使用php面向对象的DateTime::diff

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

答案 1 :(得分:0)

您可能希望使用DateTime::diff()来解决该问题。

通过使用floor(),你基本上得到下一个较低的整数(在逗号后删除所有内容)。

答案 2 :(得分:0)

所以问题是因为它跨越了夏令时。虽然PHP规范说strtotime从epoch开始返回秒数,但我想情况并非如此。

相关问题