【发布时间】:2018-07-28 00:35:57
【问题描述】:
// Difference from a date in the future:
$a = new DateTime('2000-01-01');
$b = new DateTime('2000-01-05');
$interval = $b->diff($a);
return $interval->days; // Returns 4
// Difference from a date in the past:
$a = new DateTime('2000-01-01');
$b = new DateTime('1999-12-28');
$interval = $a->diff($b); // Arguments swapped
return $interval->days; // Returns 4
为什么这两个函数都返回正 4? 如果日期是过去的,如何返回负数?
【问题讨论】:
-
Diff的返回值是一个DateInterval对象。你是怎么输出的? -
对不起,我的代码有错别字,现在更新
-
DateInterval对象具有指定它们是正数还是负数的invert属性:php.net/manual/de/class.dateinterval.php