奇怪的PHP 5.3问题与日期差异计算差异天数 [英] Strange PHP 5.3 issue with date diff calculating difference in days
问题描述
使用PHP 5.3的日期差异函数计算两个日期之间的差异,我遇到一个相当奇怪的问题。以下是我的代码:
I am experiencing a rather strange problem using PHP 5.3's date diff function to calculate the difference in days between two dates. Below is my code:
$currentDate = new DateTime(); // (today's date is 2012-1-27)
$startDate = new DateTime('2012-04-01');
$diff = $startDate->diff($currentDate);
$daysBefore = $diff->d;
echo $daysBefore;
上面的代码显示4作为$ daysBefore变量的值。
The above code displays 4 as the value of the $daysBefore variable.
为什么PHP在2012年1月27日至2012年4月1日之间显示4天的差异,那么这些日期之间显然还有更多的日子。
Why is PHP displaying a difference of 4 days between the dates 27th Jan 2012 and 1st April 2012, when clearly there are many more days between these dates.
我做错了什么?
推荐答案
DateInterval :: $ d
是间隔时间的一部分,不差异的总天数。为此,您需要 DateInterval :: $ days
,所以:
DateInterval::$d
is the days part of the interval, not the total number of days of the difference. For that, you want DateInterval::$days
, so:
$daysBefore = $diff->days;
这篇关于奇怪的PHP 5.3问题与日期差异计算差异天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!