0

Is society we use time zones, but for some reason php uses a long list of major cities for it's timezone settings. Often I do work on websites in various regions of the world and it's not easy to figure out which timezone setting is correct.

For instance, you're working on a site in Albstadt, Germany. You do a google search and find that Albstadt is GMT+1. You then go to the php documentation pages and find this: http://php.net/manual/en/timezones.europe.php

Ok, there's a list of major cities in Europe. Not very helpful. And no, even though the page is titled "List of Supported Timezones" they are in fact not "timezones". Unless you happen to know which of those cities is GMT+1, it seems you're a bit stuck doing trial and error until you get a hit. Seems like there should be a setting in php like:

date.timezone = 'GMT+1'

but there isn't. Hence my question: How can I easily determine the correct date.timezone setting for a given locality?

(note: I am aware of this Q&A: List of US Time Zones for PHP to use?, but I'm not looking for a list, nor do I consider "here is a list I found" a reliable method for determining the correct setting)

Community
  • 1
  • 1
  • 1
    Somebody, somewhere, is going to have to maintain a list since this stuff changes. I'm not sure if there is going to be a better answer than query some list or API. – NathanOliver Feb 17 '17 at 20:24
  • 5
    In particular, it's the IANA: http://www.iana.org/time-zones/repository/tz-link.html – Gordon Feb 17 '17 at 20:26
  • 2
    One issue with doing it the way you suggest is that it doesn't account for DST. For an arbitrary example, both [`America/Sao_Paulo`](http://php.net/manual/en/timezones.america.php) and `America/Bahia` are GMT -3, but `America/Sao_Paulo` follows the Brazilian DST, while `America/Bahia` doesn't. – duplode Feb 17 '17 at 20:32
  • 2
    @duplode - you are 100% right. I guess I am used to seeing things like "Eastern Standard Time" and "Greenwich Mean Time" as timezones and wasn't aware of IANA's database. – But those new buttons though.. Feb 17 '17 at 20:38
  • Please read the [timezone tag wiki](http://stackoverflow.com/tags/timezone/info). In particular, the section "Time Zone!= Offset" – Matt Johnson-Pint Feb 17 '17 at 23:23

1 Answers1

3

If you want to know the timezones in a country, you can use

print_r(
    DateTimezone::listIdentifiers(
        DateTimeZone::PER_COUNTRY, 
        'DE'
    )
);

This would give you an output like:

Array
(
    [0] => Europe/Berlin
    [1] => Europe/Busingen
)

Busingen would be closer to Albstadt, but it doesn't matter because Germany only has one timezone, which is CET (and CEST in summer), so both would be fine to use. But for countries with multiple timezones, this will only let you narrow it down, not pinpoint it.

You can also use GMT, although it's not recommended, because

Warning: Please do not use any of the timezones listed here (besides UTC), they only exist for backward compatible reasons, and may expose erroneous behavior.

However, it does work:

date_default_timezone_set('Etc/GMT+10');
echo date(DATE_ATOM); // prints 2017-02-17T10:10:24-10:00

And for CET:

date_default_timezone_set('CET');
echo date(DATE_ATOM); // 2017-02-17T21:31:35+01:00

Make sure to read through http://www.iana.org/time-zones/repository/tz-link.html for additional details. The IANA is the maintainer of the timezone database PHP uses. In particular, this bit is important:

Each location in the database represents a region where all clocks keeping local time have agreed since 1970. Locations are identified by continent or ocean and then by the name of the location, which is typically the largest city within the region. For example, America/New_York represents most of the US eastern time zone; America/Phoenix represents most of Arizona, which uses mountain time without daylight saving time (DST); America/Detroit represents most of Michigan, which uses eastern time but with different DST rules in 1975; and other entries represent smaller regions like Starke County, Indiana, which switched from central to eastern time in 1991 and switched back in 2006. To use the database on an extended POSIX implementation set the TZ environment variable to the location's full name, e.g., TZ="America/New_York".

Also note the big section titled Time zone boundaries on the same page.

You might also be interested in How to get a time zone from a location using latitude and longitude coordinates?

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • this is really great information - particularly that last link. Is php's reliance on IANA's db documented somewhere or is this just assumed? – But those new buttons though.. Feb 17 '17 at 20:43
  • 1
    @billynoah it's mentioned at http://php.net/manual/en/timezones.others.php. Also, http://php.net/manual/en/timezones.php says the latest version of the timezone database can be installed via PECL's » timezonedb (https://pecl.php.net/package/timezonedb) which states The data that this extension uses comes from the "Olson" database, which is located at http://www.iana.org/time-zones. – Gordon Feb 17 '17 at 20:47