Wrong Timestamp/Timezone conversion Wrong Timestamp/Timezone conversion symfony symfony

Wrong Timestamp/Timezone conversion


The issue comes from the fact that createFromFormat() is ignoring its third argument (time zone) by design:

Note: The timezone parameter and the current timezone are ignored when the time parameter either contains a UNIX timestamp (e.g. 946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).

Here's a cleaner test case:

$dt = new DateTime('2016-06-22 00:00:00', new DateTimeZone('Europe/Berlin'));$dt2 = DateTime::createFromFormat('U', $dt->getTimestamp(), new \DateTimeZone('Europe/Berlin'));var_dump($dt->format('c'), $dt2->format('c'));
string(25) "2016-06-22T00:00:00+02:00"string(25) "2016-06-21T22:00:00+00:00"

Here, you'd need to change time zone afterwards (default time zone is only used when no time zone information exists):

$dt2->setTimeZone(new DateTimeZone('Europe/Berlin'));