0

所以我试图得到以下日期的天数差异。其中一个有效,但另一个数字已关闭。应该是3。

我可以使用该ceil功能,但我仍然不知道为什么以下内容不正确。

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

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

3 回答 3

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');
于 2011-03-09T22:39:44.513 回答
0

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

通过使用 floor(),您实际上得到了下一个较低的整数(删除逗号后的所有内容)。

于 2011-03-09T22:37:06.843 回答
0

所以问题是因为它跨越了夏令时。尽管 PHP 规范说 strtotime 返回纪元以来的秒数,但我想情况并非如此。

于 2011-03-09T22:41:59.993 回答