TomR
TomR

Reputation: 3056

Unpredictable effect of the use of DateTimeZone PHP class?

I am trying to fetch date/timestamp data from the database and convert them into JSON/ISO8601 datetime strings with the timezone added, this is continuation of the What determines (how to configure) what format string is used by php PDO driver for values of date and timestamp fields?

I have the following test code:

        $t1 = '2019-10-10 00:00:00';
        $t2 = '1899-12-30 06:10:38'; 
        $t3 = '2019-12-30 06:10:38';
        $t4 = '2019-10-10 00:00:01';
        $dt1 = \DateTime::createFromFormat ('Y-m-d H:i:s', $t1, new \DateTimeZone('Europe/Tallinn'));
        $dt2 = \DateTime::createFromFormat ('Y-m-d H:i:s', $t2, new \DateTimeZone('Europe/Tallinn'));
        $dt3 = \DateTime::createFromFormat ('Y-m-d H:i:s', $t3, new \DateTimeZone('Europe/Tallinn'));
        $dt4 = \DateTime::createFromFormat ('Y-m-d H:i:s', $t4, new \DateTimeZone('Europe/Tallinn'));
        return $dt1->format('Y-m-d\TH:i:sO')."\r\n".$dt2->format('Y-m-d\TH:i:sO')."\r\n".$dt3->format('Y-m-d\TH:i:sO')."\r\n".$dt4->format('Y-m-d\TH:i:sO');

But result is completely unpredictable:

2019-10-10T00:00:00+0300
1899-12-30T06:10:38+0139
2019-12-30T06:10:38+0200
2019-10-10T00:00:01+0300

I provide the exact time zone (Europe/Tallinn) for each of the 4 datetime strings, but the actual timezone for the DateTime instances is different. I expect 03 or 02 in each case (I am not sure about the use of daylight saving effect), but I clearly can not expect 0139. What is happening here?

PHP 7.x.

Upvotes: 0

Views: 51

Answers (1)

Claudio
Claudio

Reputation: 5213

Those timezones are all correct, the +3 is applied on summer time (from 28th of March until 31th of October), and +2 is the normal time. About the 1899 timezone is also correct as you can verify here

1899 Timezone

Upvotes: 3

Related Questions