13

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.

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

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)
patie
  • 1,074
  • 1
  • 10
  • 18

2 Answers2

2

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

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);
Max
  • 891
  • 8
  • 17
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.

Marko
  • 514
  • 1
  • 4
  • 16