PHP Timezone List

PHP Timezone List

Take my array of time zones, which I made specially for select element. It is associated array where key is PHP time zone and value is human representation. This is it:

$timezones = array(
'Pacific/Midway' => "(GMT-11:00) Midway Island",
'US/Samoa' => "(GMT-11:00) Samoa",
'US/Hawaii' => "(GMT-10:00) Hawaii",
'US/Alaska' => "(GMT-09:00) Alaska",
'US/Pacific' => "(GMT-08:00) Pacific Time (US & Canada)",
'America/Tijuana' => "(GMT-08:00) Tijuana",
'US/Arizona' => "(GMT-07:00) Arizona",
'US/Mountain' => "(GMT-07:00) Mountain Time (US & Canada)",
'America/Chihuahua' => "(GMT-07:00) Chihuahua",
'America/Mazatlan' => "(GMT-07:00) Mazatlan",
'America/Mexico_City' => "(GMT-06:00) Mexico City",
'America/Monterrey' => "(GMT-06:00) Monterrey",
'Canada/Saskatchewan' => "(GMT-06:00) Saskatchewan",
'US/Central' => "(GMT-06:00) Central Time (US & Canada)",
'US/Eastern' => "(GMT-05:00) Eastern Time (US & Canada)",
'US/East-Indiana' => "(GMT-05:00) Indiana (East)",
'America/Bogota' => "(GMT-05:00) Bogota",
'America/Lima' => "(GMT-05:00) Lima",
'America/Caracas' => "(GMT-04:30) Caracas",
'Canada/Atlantic' => "(GMT-04:00) Atlantic Time (Canada)",
'America/La_Paz' => "(GMT-04:00) La Paz",
'America/Santiago' => "(GMT-04:00) Santiago",
'Canada/Newfoundland' => "(GMT-03:30) Newfoundland",
'America/Buenos_Aires' => "(GMT-03:00) Buenos Aires",
'Greenland' => "(GMT-03:00) Greenland",
'Atlantic/Stanley' => "(GMT-02:00) Stanley",
'Atlantic/Azores' => "(GMT-01:00) Azores",
'Atlantic/Cape_Verde' => "(GMT-01:00) Cape Verde Is.",
'Africa/Casablanca' => "(GMT) Casablanca",
'Europe/Dublin' => "(GMT) Dublin",
'Europe/Lisbon' => "(GMT) Lisbon",
'Europe/London' => "(GMT) London",
'Africa/Monrovia' => "(GMT) Monrovia",
'Europe/Amsterdam' => "(GMT+01:00) Amsterdam",
'Europe/Belgrade' => "(GMT+01:00) Belgrade",
'Europe/Berlin' => "(GMT+01:00) Berlin",
'Europe/Bratislava' => "(GMT+01:00) Bratislava",
'Europe/Brussels' => "(GMT+01:00) Brussels",
'Europe/Budapest' => "(GMT+01:00) Budapest",
'Europe/Copenhagen' => "(GMT+01:00) Copenhagen",
'Europe/Ljubljana' => "(GMT+01:00) Ljubljana",
'Europe/Madrid' => "(GMT+01:00) Madrid",
'Europe/Paris' => "(GMT+01:00) Paris",
'Europe/Prague' => "(GMT+01:00) Prague",
'Europe/Rome' => "(GMT+01:00) Rome",
'Europe/Sarajevo' => "(GMT+01:00) Sarajevo",
'Europe/Skopje' => "(GMT+01:00) Skopje",
'Europe/Stockholm' => "(GMT+01:00) Stockholm",
'Europe/Vienna' => "(GMT+01:00) Vienna",
'Europe/Warsaw' => "(GMT+01:00) Warsaw",
'Europe/Zagreb' => "(GMT+01:00) Zagreb",
'Europe/Athens' => "(GMT+02:00) Athens",
'Europe/Bucharest' => "(GMT+02:00) Bucharest",
'Africa/Cairo' => "(GMT+02:00) Cairo",
'Africa/Harare' => "(GMT+02:00) Harare",
'Europe/Helsinki' => "(GMT+02:00) Helsinki",
'Europe/Istanbul' => "(GMT+02:00) Istanbul",
'Asia/Jerusalem' => "(GMT+02:00) Jerusalem",
'Europe/Kiev' => "(GMT+02:00) Kyiv",
'Europe/Minsk' => "(GMT+02:00) Minsk",
'Europe/Riga' => "(GMT+02:00) Riga",
'Europe/Sofia' => "(GMT+02:00) Sofia",
'Europe/Tallinn' => "(GMT+02:00) Tallinn",
'Europe/Vilnius' => "(GMT+02:00) Vilnius",
'Asia/Baghdad' => "(GMT+03:00) Baghdad",
'Asia/Kuwait' => "(GMT+03:00) Kuwait",
'Africa/Nairobi' => "(GMT+03:00) Nairobi",
'Asia/Riyadh' => "(GMT+03:00) Riyadh",
'Europe/Moscow' => "(GMT+03:00) Moscow",
'Asia/Tehran' => "(GMT+03:30) Tehran",
'Asia/Baku' => "(GMT+04:00) Baku",
'Europe/Volgograd' => "(GMT+04:00) Volgograd",
'Asia/Muscat' => "(GMT+04:00) Muscat",
'Asia/Tbilisi' => "(GMT+04:00) Tbilisi",
'Asia/Yerevan' => "(GMT+04:00) Yerevan",
'Asia/Kabul' => "(GMT+04:30) Kabul",
'Asia/Karachi' => "(GMT+05:00) Karachi",
'Asia/Tashkent' => "(GMT+05:00) Tashkent",
'Asia/Kolkata' => "(GMT+05:30) Kolkata",
'Asia/Kathmandu' => "(GMT+05:45) Kathmandu",
'Asia/Yekaterinburg' => "(GMT+06:00) Ekaterinburg",
'Asia/Almaty' => "(GMT+06:00) Almaty",
'Asia/Dhaka' => "(GMT+06:00) Dhaka",
'Asia/Novosibirsk' => "(GMT+07:00) Novosibirsk",
'Asia/Bangkok' => "(GMT+07:00) Bangkok",
'Asia/Jakarta' => "(GMT+07:00) Jakarta",
'Asia/Krasnoyarsk' => "(GMT+08:00) Krasnoyarsk",
'Asia/Chongqing' => "(GMT+08:00) Chongqing",
'Asia/Hong_Kong' => "(GMT+08:00) Hong Kong",
'Asia/Kuala_Lumpur' => "(GMT+08:00) Kuala Lumpur",
'Australia/Perth' => "(GMT+08:00) Perth",
'Asia/Singapore' => "(GMT+08:00) Singapore",
'Asia/Taipei' => "(GMT+08:00) Taipei",
'Asia/Ulaanbaatar' => "(GMT+08:00) Ulaan Bataar",
'Asia/Urumqi' => "(GMT+08:00) Urumqi",
'Asia/Irkutsk' => "(GMT+09:00) Irkutsk",
'Asia/Seoul' => "(GMT+09:00) Seoul",
'Asia/Tokyo' => "(GMT+09:00) Tokyo",
'Australia/Adelaide' => "(GMT+09:30) Adelaide",
'Australia/Darwin' => "(GMT+09:30) Darwin",
'Asia/Yakutsk' => "(GMT+10:00) Yakutsk",
'Australia/Brisbane' => "(GMT+10:00) Brisbane",
'Australia/Canberra' => "(GMT+10:00) Canberra",
'Pacific/Guam' => "(GMT+10:00) Guam",
'Australia/Hobart' => "(GMT+10:00) Hobart",
'Australia/Melbourne' => "(GMT+10:00) Melbourne",
'Pacific/Port_Moresby' => "(GMT+10:00) Port Moresby",
'Australia/Sydney' => "(GMT+10:00) Sydney",
'Asia/Vladivostok' => "(GMT+11:00) Vladivostok",
'Asia/Magadan' => "(GMT+12:00) Magadan",
'Pacific/Auckland' => "(GMT+12:00) Auckland",
'Pacific/Fiji' => "(GMT+12:00) Fiji",
);

Generating a drop down list of timezones with PHP

I would do it in PHP, except I would avoid doing preg_match 100 some times and do this to generate your list.

$tzlist = DateTimeZone::listIdentifiers(DateTimeZone::ALL);

Also, I would use PHP's names for the 'timezones' and forget about GMT offsets, which will change based on DST.
Code like that in phpbb is only that way b/c they are still supporting PHP4 and can't rely on the DateTime or DateTimeZone objects being there.

List of US Time Zones for PHP to use?

Here is a list I found:

Eastern Time    America/New_York
Central Time America/Chicago
Mountain Time America/Denver
Mountain Time (no DST) America/Phoenix
Pacific Time America/Los_Angeles
Alaska Time America/Anchorage
Hawaii-Aleutian America/Adak
Hawaii-Aleutian Time (no DST) Pacific/Honolulu

Get all timezones

You can use IntlTimeZone::getDisplayName(), e.g.:

echo IntlTimeZone::createTimeZone("Australia/Darwin")->getDisplayName();

Outputs:

Australian Central Standard Time

Though many will change throughout the year for daylight saving. If you want to check for this and add the alternate daylight/summer display name you can do something like:

$tz = IntlTimeZone::createTimeZone("America/Los_Angeles");
echo $tz->getDisplayName(), "\n";
if ($tz->useDaylightTime()) {
echo $tz->getDisplayName(true);
}

which outputs:

Pacific Standard Time
Pacific Daylight Time

PHP - Filters List of Timezone as Array

This is my solution. I have an hardcoded array with the timezone identifier and display (for frontend use), which I use to generate an array with the right offset displays.

    static function generateTimeZoneList()
{
static $timezone_list;

if(!isset($timezone_list) || empty($timezone_list))
{
$identifiers = self::getIdentifiers(); //get identifiers list

$temp_list = array();
foreach( $identifiers as $timezone => $display )
{
$tz = new DateTimeZone($timezone);
$temp_list[$timezone] = $tz->getOffset(new DateTime);
}
// sort timezone by offset
asort($temp_list);

foreach($temp_list as $timezone => $offset)
{
$offset_prefix = $offset < 0 ? '-' : '+';
$offset_formatted = gmdate( 'H:i', abs($offset) );

$pretty_offset = "GMT${offset_prefix}${offset_formatted}";

$display = $identifiers[$timezone];
$timezone_list[$timezone] = "($pretty_offset) $display";
}
}
return $timezone_list;
}

/**
* Format: identifier => display name
*/
static function getIdentifiers()
{
static $identifiers = array(
"Africa/Cairo" => " Cairo",
"Africa/Casablanca" => " Casablanca",
"Africa/Harare" => " Harare",
"Africa/Johannesburg" => " Pretoria",
"Africa/Lagos" => " West Central Africa",
"Africa/Monrovia" => " Monrovia",
"Africa/Nairobi" => " Nairobi",
"America/Argentina/Buenos_Aires" => " Buenos Aires",
"America/Argentina/Buenos_Aires" => " Georgetown",
"America/Bogota" => " Quito",
"America/Bogota" => " Bogota",
"America/Caracas" => " Caracas",
"America/Chihuahua" => " La Paz",
"America/Chihuahua" => " Chihuahua",
"America/Godthab" => " Greenland",
"America/La_Paz" => " La Paz",
"America/Lima" => " Lima",
"America/Los_Angeles" => " Pacific Time (US & Canada)",
"America/Managua" => " Central America",
"America/Mazatlan" => " Mazatlan",
"America/Mexico_City" => " Mexico City",
"America/Mexico_City" => " Guadalajara",
"America/Monterrey" => " Monterrey",
"America/Noronha" => " Mid-Atlantic",
"America/Santiago" => " Santiago",
"America/Sao_Paulo" => " Brasilia",
"America/Tijuana" => " Tijuana",
"Asia/Almaty" => " Almaty",
"Asia/Baghdad" => " Baghdad",
"Asia/Baku" => " Baku",
"Asia/Bangkok" => " Hanoi",
"Asia/Bangkok" => " Bangkok",
"Asia/Calcutta" => " Chennai",
"Asia/Calcutta" => " Mumbai",
"Asia/Calcutta" => " New Delhi",
"Asia/Calcutta" => " Sri Jayawardenepura",
"Asia/Chongqing" => " Chongqing",
"Asia/Dhaka" => " Dhaka",
"Asia/Dhaka" => " Astana",
"Asia/Hong_Kong" => " Beijing",
"Asia/Hong_Kong" => " Hong Kong",
"Asia/Irkutsk" => " Irkutsk",
"Asia/Jakarta" => " Jakarta",
"Asia/Jerusalem" => " Jerusalem",
"Asia/Kabul" => " Kabul",
"Asia/Kamchatka" => " Kamchatka",
"Asia/Karachi" => " Karachi",
"Asia/Karachi" => " Islamabad",
"Asia/Katmandu" => " Kathmandu",
"Asia/Kolkata" => " Kolkata",
"Asia/Krasnoyarsk" => " Krasnoyarsk",
"Asia/Kuala_Lumpur" => " Kuala Lumpur",
"Asia/Kuwait" => " Kuwait",
"Asia/Magadan" => " Solomon Is.",
"Asia/Magadan" => " Magadan",
"Asia/Magadan" => " New Caledonia",
"Asia/Muscat" => " Abu Dhabi",
"Asia/Muscat" => " Muscat",
"Asia/Novosibirsk" => " Novosibirsk",
"Asia/Rangoon" => " Rangoon",
"Asia/Riyadh" => " Riyadh",
"Asia/Seoul" => " Seoul",
"Asia/Singapore" => " Singapore",
"Asia/Taipei" => " Taipei",
"Asia/Tashkent" => " Tashkent",
"Asia/Tbilisi" => " Tbilisi",
"Asia/Tehran" => " Tehran",
"Asia/Tokyo" => " Osaka",
"Asia/Tokyo" => " Tokyo",
"Asia/Tokyo" => " Sapporo",
"Asia/Ulan_Bator" => " Ulaan Bataar",
"Asia/Urumqi" => " Urumqi",
"Asia/Vladivostok" => " Vladivostok",
"Asia/Yakutsk" => " Yakutsk",
"Asia/Yekaterinburg" => " Ekaterinburg",
"Asia/Yerevan" => " Yerevan",
"Atlantic/Azores" => " Azores",
"Atlantic/Cape_Verde" => " Cape Verde Is.",
"Australia/Adelaide" => " Adelaide",
"Australia/Brisbane" => " Brisbane",
"Australia/Canberra" => " Canberra",
"Australia/Darwin" => " Darwin",
"Australia/Hobart" => " Hobart",
"Australia/Melbourne" => " Melbourne",
"Australia/Perth" => " Perth",
"Australia/Sydney" => " Sydney",
"Canada/Atlantic" => " Atlantic Time (Canada)",
"Canada/Newfoundland" => " Newfoundland",
"Canada/Saskatchewan" => " Saskatchewan",
"Etc/Greenwich" => " Greenwich Mean Time : Dublin",
"Europe/Amsterdam" => " Amsterdam",
"Europe/Athens" => " Athens",
"Europe/Belgrade" => " Belgrade",
"Europe/Berlin" => " Berlin",
"Europe/Berlin" => " Bern",
"Europe/Bratislava" => " Bratislava",
"Europe/Brussels" => " Brussels",
"Europe/Bucharest" => " Bucharest",
"Europe/Budapest" => " Budapest",
"Europe/Copenhagen" => " Copenhagen",
"Europe/Helsinki" => " Helsinki",
"Europe/Helsinki" => " Kyiv",
"Europe/Istanbul" => " Istanbul",
"Europe/Lisbon" => " Lisbon",
"Europe/Ljubljana" => " Ljubljana",
"Europe/London" => " Edinburgh",
"Europe/London" => " London",
"Europe/Madrid" => " Madrid",
"Europe/Minsk" => " Minsk",
"Europe/Moscow" => " St. Petersburg",
"Europe/Moscow" => " Moscow",
"Europe/Paris" => " Paris",
"Europe/Prague" => " Prague",
"Europe/Riga" => " Riga",
"Europe/Rome" => " Rome",
"Europe/Sarajevo" => " Sarajevo",
"Europe/Skopje" => " Skopje",
"Europe/Sofia" => " Sofia",
"Europe/Stockholm" => " Stockholm",
"Europe/Tallinn" => " Tallinn",
"Europe/Vienna" => " Vienna",
"Europe/Vilnius" => " Vilnius",
"Europe/Volgograd" => " Volgograd",
"Europe/Warsaw" => " Warsaw",
"Europe/Zagreb" => " Zagreb",
"Pacific/Auckland" => " Wellington",
"Pacific/Auckland" => " Auckland",
"Pacific/Fiji" => " Fiji",
"Pacific/Fiji" => " Marshall Is.",
"Pacific/Guam" => " Guam",
"Pacific/Honolulu" => " Hawaii",
"Pacific/Kwajalein" => " International Date Line West",
"Pacific/Midway" => " Midway Island",
"Pacific/Port_Moresby" => " Port Moresby",
"Pacific/Samoa" => " Samoa",
"Pacific/Tongatapu" => " Nuku'alofa",
"US/Alaska" => " Alaska",
"US/Arizona" => " Arizona",
"US/Central" => " Central Time (US & Canada)",
"US/East-Indiana" => " Indiana (East)",
"US/Eastern" => " Eastern Time (US & Canada)",
"US/Mountain" => " Mountain Time (US & Canada)",
"UTC" => " UTC"
);

return $identifiers;
}

The result looks similar to this,

array (size=121)
'Pacific/Midway' => string '(GMT-11:00) Midway Island' (length=26)
'Pacific/Samoa' => string '(GMT-11:00) Samoa' (length=18)
'Pacific/Honolulu' => string '(GMT-10:00) Hawaii' (length=19)
'US/Alaska' => string '(GMT-08:00) Alaska' (length=19)
'America/Los_Angeles' => string '(GMT-07:00) Pacific Time (US & Canada)' (length=39)
'US/Arizona' => string '(GMT-07:00) Arizona' (length=20)
'America/Tijuana' => string '(GMT-07:00) Tijuana' (length=20)
'Canada/Saskatchewan' => string '(GMT-06:00) Saskatchewan' (length=25)
'America/Managua' => string '(GMT-06:00) Central America' (length=28)
'America/Mazatlan' => string '(GMT-06:00) Mazatlan' (length=21)
'America/Chihuahua' => string '(GMT-06:00) Chihuahua' (length=22)
'US/Mountain' => string '(GMT-06:00) Mountain Time (US & Canada)' (length=40)
'America/Lima' => string '(GMT-05:00) Lima' (length=17)

Php timezones and list visited

You should set date.timezone to UTC in php.ini if you have access to it, if not then do it in .htaccess like this:-

php_value date.timezone UTC

Your application will now default to using UTC as the timezone unless you specify otherwise.
You can store user timezones using the strings in this list. Just allow your users to choose the correct one from a drop down.

Your application should store all date/times as UTC using either a unix or mysql timestamp. My personal preference is for unix timestamps.

When presenting dates to the user make use of the DateTime classes. For example:-

$timeZoneString = getTimeZoneFromDB();
$tz = new \DateTimeZone($timeZoneString);
$date = (new \DateTime())->setTimeZone($tz);
echo $date->format('Y-m-d H:i:s);

You should not use date_default_timezone_set() to modify timezones on the fly as this can have unpredictable side affects in your app. If that particular function is used at all, it should be only once in your app. If it appears more than that, you should consider it a code smell.

PHP Version which includes Timezone Asia/Beijing?

This isn't really so much dependent on the PHP version, but on what timezone database it uses.

As https://www.php.net/manual/en/timezones.php states,

Note: This list is based upon the timezone database version 2021.5.

and

Note: The latest version of the timezone database can be installed via PECL's » timezonedb."



Related Topics



Leave a reply



Submit