PHP 错误的 DateTime::diff() 返回错误的 DateInterval

rga*_*lot 4 php datetime dateinterval

我遇到两个日期时间相差的问题。这是显示 DateInterval 对象的命令行:

php -r "\$a = new Datetime('first day of 4 months ago midnight'); \$b = new Datetime('first day of 1 month ago midnight'); var_dump(\$a->diff(\$b));"
Run Code Online (Sandbox Code Playgroud)

这里是DateInterval输出:

class DateInterval#3 (15) {
  public $y =>      int(0)
  public $m =>      int(3)
  public $d =>      int(3)
  public $h =>      int(0)
  public $i =>      int(0)
  public $s =>      int(0)
  public $weekday =>               int(0)
  public $weekday_behavior =>      int(0)
  public $first_last_day_of =>     int(0)
  public $invert =>                int(0)
  public $days =>                  int(92)
  public $special_type =>               int(0)
  public $special_amount =>             int(0)
  public $have_weekday_relative =>      int(0)
  public $have_special_relative =>      int(0)
}
Run Code Online (Sandbox Code Playgroud)

编辑:第一个和第二个日期时间:

class DateTime#1 (3) {
  public $date =>
  string(19) "2014-03-01 00:00:00"
  public $timezone_type =>
  int(3)
  public $timezone =>
  string(13) "Europe/Zurich"
}

class DateTime#2 (3) {
  public $date =>
  string(19) "2014-06-01 00:00:00"
  public $timezone_type =>
  int(3)
  public $timezone =>
  string(13) "Europe/Zurich"
}
Run Code Online (Sandbox Code Playgroud)

注意这3天!我使用的是 PHP 5.5.8,但我确信几天前这个 DateInterval 有 0 个月。PHP 5.4.28 和 5.5.14 中的 DateInterval 输出 0 天。我不确定PHP版本有没有效果。

在这两种情况下,days 属性都是 92。

zam*_*uts 5

深入了解Paul T. Rawkeen 的答案,问题在于DateTime::diff在计算之前首先将时区转换为 UTC

<?php

$zurich = new DateTimeZone('Europe/Zurich');
$utc = new DateTimeZone('UTC');

$a = new DateTime('first day of 4 months ago midnight',$zurich);
$b = new DateTime('first day of 1 month ago midnight',$zurich);

var_dump($a,$b);

$a->setTimezone($utc);
$b->setTimezone($utc);

var_dump($a,$b);

?>
Run Code Online (Sandbox Code Playgroud)

给出以下内容:

object(DateTime)[3]
  public 'date' => string '2014-03-01 00:00:00' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'Europe/Zurich' (length=13)
object(DateTime)[4]
  public 'date' => string '2014-06-01 00:00:00' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'Europe/Zurich' (length=13)
object(DateTime)[3]
  public 'date' => string '2014-02-28 23:00:00' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'UTC' (length=3)
object(DateTime)[4]
  public 'date' => string '2014-05-31 22:00:00' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'UTC' (length=3)
Run Code Online (Sandbox Code Playgroud)

Europe/Zurich在将时区分别转换为现在和过去UTC的日期后,3 天的差异现在非常明显。2014-02-28 23:00:002014-05-31 22:00:00$a$b


解决方案是完全使用 UTC 并在显示日期时间之前进行转换:

<?php

$zurich = new DateTimeZone('Europe/Zurich');
$utc = new DateTimeZone('UTC');

$a = new DateTime('first day of 4 months ago midnight',$utc);
$b = new DateTime('first day of 1 month ago midnight',$utc);

var_dump($a,$b);

$a->setTimezone($zurich);
$b->setTimezone($zurich);

var_dump($a,$b);

?>
Run Code Online (Sandbox Code Playgroud)

请注意,所有日子都是 now 01,尽管现在的时间略有不同(请参阅本答案末尾的注释):

object(DateTime)[3]
  public 'date' => string '2014-03-01 00:00:00' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'UTC' (length=3)
object(DateTime)[4]
  public 'date' => string '2014-06-01 00:00:00' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'UTC' (length=3)
object(DateTime)[3]
  public 'date' => string '2014-03-01 01:00:00' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'Europe/Zurich' (length=13)
object(DateTime)[4]
  public 'date' => string '2014-06-01 02:00:00' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'Europe/Zurich' (length=13)
Run Code Online (Sandbox Code Playgroud)

为了深入了解这一现象,请注意以下几点:

  • 二月有28天(2014年)
  • 五月有31天
  • DST 于 3 月 30 日开始:+01:00
  • 欧洲/苏黎世时间为 UTC+02:00

从欧洲/苏黎世转换为 UTC 时,不仅要考虑年、月和日,还要考虑小时、分钟和秒。如果这是任何一个月第一天以外的任何一天,则不会出现此问题,但时间仍为 23:00 和 22:00(在上面的示例之前)。