DateDiff为每个时区返回不同的结果

时间:2021-07-20 01:36:47

I have problem with PHP DateDiff, i dont understand why each timezone returns different results, for example in this case Prague return 0 month, and US return 1 month.

我有PHP DateDiff的问题,我不明白为什么每个时区返回不同的结果,例如在这种情况下布拉格返回0个月,美国返回1个月。

What do this difference and how i return 1 month (instead 30 days, when i adding 1 month) as expected?

这个差异是什么以及如何按预期返回1个月(而不是30天,当我加1个月时)?

code Europe/Prague:

date_default_timezone_set("Europe/Prague");
$from = new \DateTimeImmutable('2016-09-01');
$to = $from->add(new \DateInterval('P1M'));
var_dump($from);
var_dump($to);
var_dump($from->diff($to)->m);
var_dump($from->diff($to)->d);

result Europe/Prague:

object(DateTimeImmutable)#1 (3) {
  ["date"]=>
  string(26) "2016-09-01 00:00:00.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(13) "Europe/Prague"
}
object(DateTimeImmutable)#3 (3) {
  ["date"]=>
  string(26) "2016-10-01 00:00:00.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(13) "Europe/Prague"
}
int(0)
int(30)

--

code US/Pacific:

date_default_timezone_set("US/Pacific");
$from = new \DateTimeImmutable('2016-09-01');
$to = $from->add(new \DateInterval('P1M'));
var_dump($from);
var_dump($to);
var_dump($from->diff($to)->m);
var_dump($from->diff($to)->d);

result US/Pacific:

object(DateTimeImmutable)#2 (3) {
  ["date"]=>
  string(26) "2016-09-01 00:00:00.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(10) "US/Pacific"
}
object(DateTimeImmutable)#4 (3) {
  ["date"]=>
  string(26) "2016-10-01 00:00:00.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(10) "US/Pacific"
}
int(1)
int(0)

2 个解决方案

#1


1  

This is indeed a small bug in PHP DateTime class. You must use the UTC timezone and set the desired timezone after the calculation :

这确实是PHP DateTime类中的一个小错误。您必须使用UTC时区并在计算后设置所需的时区:

date_default_timezone_set('UTC');

$europePrag = new DateTimeZone('Europe/Prague');
$usPacific = new DateTimeZone('US/Pacific');

$from = new \DateTimeImmutable('2016-11-01');
$to = $from->add(new \DateInterval('P1M'));
$from->setTimezone($europePrag);
var_dump($from);
var_dump($to);
var_dump($from->diff($to)->m);
var_dump($from->diff($to)->d);


$from = new \DateTimeImmutable('2016-11-01');
$to = $from->add(new \DateInterval('P1M'));
$from->setTimezone($usPacific);
var_dump($from);
var_dump($to);
var_dump($from->diff($to)->m);
var_dump($from->diff($to)->d);

#2


0  

I think it is the behaviour described in this ticket:

我认为这是此票证中描述的行为:

https://bugs.php.net/bug.php?id=52480

So, yes it seems to be a bug in PHP.

所以,是的,它似乎是PHP中的一个错误。

#1


1  

This is indeed a small bug in PHP DateTime class. You must use the UTC timezone and set the desired timezone after the calculation :

这确实是PHP DateTime类中的一个小错误。您必须使用UTC时区并在计算后设置所需的时区:

date_default_timezone_set('UTC');

$europePrag = new DateTimeZone('Europe/Prague');
$usPacific = new DateTimeZone('US/Pacific');

$from = new \DateTimeImmutable('2016-11-01');
$to = $from->add(new \DateInterval('P1M'));
$from->setTimezone($europePrag);
var_dump($from);
var_dump($to);
var_dump($from->diff($to)->m);
var_dump($from->diff($to)->d);


$from = new \DateTimeImmutable('2016-11-01');
$to = $from->add(new \DateInterval('P1M'));
$from->setTimezone($usPacific);
var_dump($from);
var_dump($to);
var_dump($from->diff($to)->m);
var_dump($from->diff($to)->d);

#2


0  

I think it is the behaviour described in this ticket:

我认为这是此票证中描述的行为:

https://bugs.php.net/bug.php?id=52480

So, yes it seems to be a bug in PHP.

所以,是的,它似乎是PHP中的一个错误。