Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateDiff return different result for each timezones

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)
like image 483
patie Avatar asked Sep 05 '16 15:09

patie


1 Answers

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);
like image 72
Max Avatar answered Nov 10 '22 22:11

Max