My PHP Installation (7.4.22) returns the correct time for other timezones, but does not return the correct time for PDT. My Server (Ubuntu 18.04.2) Shows the correct time in UTC when logging in.
Example Script:-
<?php
$date_str_8601 = '2021-10-03T12:30:00Z'; // Input 12:30 UTC
$dateTime = DateTime::createFromFormat(DateTime::ISO8601, $date_str_8601);
date_timezone_set($dateTime, timezone_open('PDT'));
echo 'PDT ' . date_format($dateTime, 'g:i A'); // Expected: PDT 5:30 AM
date_timezone_set($dateTime, timezone_open('Europe/London'));
echo ', London ' . date_format($dateTime, 'g:i A'); // Expected: London 1:30 PM
Expected Output:
PDT 5:30 AM, London 1:30 PM
Actual Output
PDT 4:30 AM, London 1:30 PM
Why is my code giving me the wrong time for PDT? Is there something about my configuration I can fix?
CodePudding user response:
PDT
is not a supported time zone name. Use America/Los_Angeles
instead.
See DateTimeZone, Timezones America and List of US Time Zones for PHP to use?
[Update]
PHP supports a list of time zone abbreviations, which includes PDT. This list appears to map to all the supported time zones that use that abbreviation, thus PDT
maps to time zone information for America/Los Angeles, America/Boise, and Asia/Manila, amongst others. These three places are in different time zones.
Further, the abbreviations list is compiled in for performance reasons, and won't reflect any updates to the system time zone database.
I haven't been able to ascertain exactly how PHP selects the timezone data from this abbreviation list. Suffice it to say that it's clear that it can't be relied on, and the full format name is the way to go.