【发布时间】:2012-02-20 12:34:05
【问题描述】:
我在使用 PHP 5.3 的日期差异函数来计算两个日期之间的天数差异时遇到了一个相当奇怪的问题。以下是我的代码:
$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 变量的值。
为什么 PHP 显示 2012 年 1 月 27 日和 2012 年 4 月 1 日之间有 4 天的差异,而这两个日期之间显然还有很多天。
我做错了吗?
【问题讨论】: