0

有人可以解释一下这个“逻辑”吗?

$datetime1 = date_create('2012-04-01');
$datetime2 = date_create('2012-05-01');
$interval = date_diff($datetime1, $datetime2);
print_r($interval);

返回 0m(月)和 30d(天)。但:

$datetime1 = date_create('2012-05-01');
$datetime2 = date_create('2012-06-01');
$interval = date_diff($datetime1, $datetime2);
print_r($interval);

返回 1m(月)和 1d(天)。

(PHP 5.3.15,LC_ALL en_US)

更新

我使用 php_value date.timezone Europe/Amsterdam,这似乎有所作为。仍然认为这很奇怪,不管是不是东部时间,它应该只给 1 个月。正确的?

http://sandbox.onlinephpfunctions.com/code/f28914a13f4047c79a19bae70570029a39196148

4

1 回答 1

0

来自 PHP 文档:

The DateInterval::format() method does not recalculate carry over points in
time strings nor in date segments. This is expected because it is not possible
to overflow values like"32 days" which could be interpreted as anything from
"1 month and 4 days" to "1 month and 1 day".

http://php.net/manual/en/dateinterval.format.php

虽然我确实觉得奇怪的是不同版本的 PHP 确实不同的方式处理它

于 2012-12-04T21:26:21.737 回答