Join David Powers for an in-depth discussion in this video Finding the correct time zone from an abbreviation, part of PHP Date and Time Essential Training.
- As explained in chapter one, PHP uses time zones that are based on geographic region rather than abbreviation, such as CET or EST. But some common abbreviations are supported. You can find them in the online documentation in this List of Supported Timezones. They're all listed under Others, so if we switch to that page and then scroll down, we can see some common abbreviations here, CET for Central European Time and EST for Eastern Standard Time, but if we scroll down further, we see this warning.
"Please do not use any of the timezones listed here "(besides UTC), they only exist "for backward compatible reasons." Fortunately, there's a relatively simple way to find the correct geographic time zone from an abbreviation. To show you how to do that, I'm going to switch to my editing program and the DateTimeZone class has a static method called listAbbreviations. This returns an associative array of more than 370 time zone abbreviations, including both those in current use and historical ones.
Because it's a static method, you need to use the class name followed by the scope resolution operator, two colons and then listAbbreviations. Each top level element of the array returned by listAbbreviations is a time zone abbreviation in lower case. So to find the abbreviation that you want use the abbreviation in lower case in square brackets after the array name. So I'm using CET for Central European Time here and if we load this page in a browser we can see the results.
It's an indexed array of all geographic locations that have used the abbreviation at some time or another. It doesn't necessarily mean it's the current time zone. If you use an abbreviation as the time zone, PHP treats it the same as the first place listed. So in the case of CET, it will use Europe/Berlin. If your location is in the same time zone as the first place listed, using the abbreviation will work for current dates.
However, for historical dates, this usually results in the incorrect application of daylight saving time. If we scroll down, we can see that CET is also applied to some locations in North Africa. Then there's this one in the Arctic and then another one in the Atlantic and if we continue scrolling down, we find Europe/Gibraltar, and then a bit further down Europe/Lisbon. These are two good cases in point with regard to historical use.
Gibraltar switched to CET in 1957 but it didn't apply daylight saving time until 1982, and Lisbon was on West European Time for a very long time, that's one hour behind CET, it didn't switch to CET until 1992. So although using geographical time zone identifiers is less familiar than using abbreviations, it's much more accurate. PHP automatically takes care of local shifts in time zone and daylight saving.
To find the correct geographic identifier from an abbreviation, use the DateTimeZone::listAbbreviations static method to get an array of all time zone abbreviations. Then use the abbreviation in lower case as the array element to display all geographic identifiers associated with that abbreviation. Finally, search through the list of results to find the correct geographic identifier for the continent and closest city in the time zone that you're looking for.
- How PHP handles dates and time
- Setting the default time zone
- Displaying current date and time
- Getting a timestamp from text
- Using the DateTime class
- Getting and modifying a timestamp from DateTime
- Comparing DateTime objects
- Working with time zones
- Using DateInterval and DatePeriod
- Finding ISO week dates
- Calculating sunrise and sunset