- Fetch all the available time zones in PHP
- The DateTimeZone class
- Fetch all of the timezones
- Fetch continent-based timezones
- How to Get a List of Time Zones in PHP?
- Get a List of All Available Time Zones Per Country
- Single or Groups of Time Zones Based on DateTimeZone Class Constants
- Get all time zones php
- Get list of all supported TimeZones and offsets in PHP
- The DateTimeZone Class
- DateTimeZone::getLocation
- DateTimeZone::getOffset
- DateTimeZone::getTransitions
- DateTimeZone::listAbbreviations
- DateTimeZone::listIdentifiers
- Get time zone identifiers for a specific region
- Get time zone identifier by country
Fetch all the available time zones in PHP
When you’re building a user-facing application, one of the most common scenarios is to let the user decide their respective time zone.
For this, you would need the list of every time zone available country/continent-wise.
Now, one way of doing this is by creating a hard-coded list of time zones and showing them as a list.
And if you’re using PHP, you can fetch all the time zones with a single line of code.
The DateTimeZone class
PHP comes with a handy class called DateTimeZone (available from PHP 5 and older) that can be used for various time zone related needs.
The class comes with several class constants and methods that can be used to retrieve various timezone-related information.
Fetch all of the timezones
So, if we want to retrieve all of the supported time zones in PHP, here’s how we can do it.
var_dump( DateTimeZone::listIdentifiers( DateTimeZone::ALL ) ); /* array(425) < [0]=>string(14) "Africa/Abidjan" [1]=> string(12) "Africa/Accra" [2]=> string(18) "Africa/Addis_Ababa" [3]=> string(14) "Africa/Algiers" [4]=> string(13) "Africa/Asmara" . > */
As you can tell, passing in DateTimeZone::ALL to the DateTimeZone::listIdentifiers method will return an array containing all the supported time zones including the UTC time zone.
Fetch continent-based timezones
If you want to retrieve the time zone of a specific continent (e.g. America), here’s how you can do it.
var_dump( DateTimeZone::listIdentifiers( DateTimeZone::AMERICA ) ); /* array(147) < [0]=>string(12) "America/Adak" [1]=> string(17) "America/Anchorage" [2]=> string(16) "America/Anguilla" [3]=> string(15) "America/Antigua" [4]=> string(17) "America/Araguaina" . > */
As you can tell, this will only return the time zones which fall under the American continent.
The available options for this are as below.
- DateTimeZone::AFRICA
- Africa time zones.
- America time zones.
- Antarctica time zones.
- Arctic time zones.
- Asia time zones.
- Atlantic time zones.
- Australia time zones.
- Europe time zones.
- Indian time zones.
- Pacific time zones.
- UTC time zones.
- All time zones.
- All time zones including backwards compatible.
- Time zones per country.
Learn the fundamentals of PHP 8 (including 8.1 and 8.2), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It’s a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you’re looking for a quick and easy way to PHP 8, this is the book for you.
👋 Hi there! I’m Amit. I write articles about all things web development. If you like what I write and want me to continue doing the same, I would like you buy me some coffees. I’d highly appreciate that. Cheers!
How to Get a List of Time Zones in PHP?
To create a list of all time zones supported by PHP, you can pass DateTimeZone::ALL to DateTimeZone::listIdentifiers() as the first argument, for example, in the following way:
$timeZones = DateTimeZone::listIdentifiers(DateTimeZone::ALL); foreach ($timeZones as $timeZone)
Get a List of All Available Time Zones Per Country
You can specify the second argument to the DateTimeZone::listIdentifiers() method as a two-letter ISO 3166-1 compatible country code to get a list of all time zones available per country. However, this only works when the first argument to the method is DateTimeZone::PER_COUNTRY . For example:
$timezones = DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, ‘US’); foreach ($timezones as $timezone)
Single or Groups of Time Zones Based on DateTimeZone Class Constants
The DateTimeZone class supports the following constants:
- DateTimeZone::AFRICA ;
- DateTimeZone::AMERICA ;
- DateTimeZone::ANTARCTICA ;
- DateTimeZone::ARCTIC ;
- DateTimeZone::ASIA ;
- DateTimeZone::ATLANTIC ;
- DateTimeZone::AUSTRALIA ;
- DateTimeZone::EUROPE ;
- DateTimeZone::INDIAN ;
- DateTimeZone::PACIFIC ;
- DateTimeZone::UTC .
Using those, you can get time zones for a single region like so:
$timeZones = DateTimeZone::listIdentifiers(DateTimeZone::AMERICA); foreach ($timeZones as $timeZone)
These DateTimeZone constants can also be combined together, for example, in the following way:
$timeZones = DateTimeZone::listIdentifiers(DateTimeZone::AMERICA|DateTimeZone::ASIA); foreach ($timeZones as $timeZone)
Hope you found this post useful. It was published 03 Jan, 2021 . Please show your love and support by sharing this post.
Get all time zones php
Get list of all supported TimeZones and offsets in PHP
Dealing with multiple timezones for users is a problem that is (for starters) best addressed by using UTC time with an offset of 0 as the server time. The next part of the problem is how to get a sane list of zones for your user to select from that you know your server supports. You could copy/paste a list, or deal with large clunky classes in frameworks — or, you could use very little code that produces a json encoded object containing all the information about each supported timezone that is required.
// tell browser the content type is json consumable
header ( ‘Content-Type: application/json’ ) ;?php>
// set the default timezone to UTC for this script.
date_default_timezone_set ( «UTC» ) ;// loop through timezones
foreach ( timezone_identifiers_list ( ) as $zone ) {// set default continent and city
$parts = array ( ‘*’ , $zone ) ;// if zone is in a continent, set the values to $parts
if ( strpos ( $zone , ‘/’ ) == true ) {
$parts = explode ( ‘/’ , $zone ) ;
}// calculate offset based on the default timezone
$offset = timezone_offset_get ( new DateTimeZone ( $zone ) , new DateTime ( ) ) ;// get country, gps coords, etc
$location = timezone_location_get ( new DateTimeZone ( $zone ) ) ;// short iso letters for zone ( eg: PDT or EST )
$abbr = ( new DateTime ( «now» , new DateTimeZone ( $zone ) ) ) -> format ( ‘T’ ) ;// add the zone information to the collection
$zones [ $parts [ 0 ] ] [ $parts [ 1 ] ] = array (
‘zone’ => $zone ,
‘offset’ => $offset ,
‘location’ => $location ,
‘abbr’ => $abbr
) ;// output result
echo json_encode ( $zones ) ;The DateTimeZone Class
This tutorial describes how to represent times in different parts of the world.
The DateTimeZone class works with the DateTime class. The DateTime constructor takes an optional second argument, which is a DateTimeZone instance. The PHP’s default time zone applies if the second argument is not provided.
Example: Specifying timezone in the constructor
To get the date and time of a specific time zone, pass a valid timezone identifier into the DateTimeZone class constructor:
$timezone = new DateTimeZone('Australia/Adelaide'); $date = new DateTime('now', $timezone); echo $date->format('d M Y, H:i:s') . '
'; # 13 Aug 2022, 13:44:31If you omit the constructor’s second argument, the time zone is determined by PHP’s default timezone setting:
format('d M Y, H:i:s') . '
'; # 18 Aug 2022, 15:16:29 $timezone = new DateTimeZone('Australia/Adelaide'); $date = new DateTime('now', $timezone); echo $date->format('d M Y, H:i:s'); # 18 Aug 2022, 22:46:29Example: DateTime::setTimezone() method to specify timezone later
You can specify or change the time zone of a DateTime object later by passing a DateTimeZone object as an argument to the setTimezone() method:
format('d M Y, H:i:s') . '
'; # 13 Aug 2022, 13:44:31 $timezone = new DateTimeZone('America/Eirunepe'); $date->setTimezone($timezone); echo $date->format('d M Y, H:i:s'); # 12 Aug 2022, 23:14:31Example: DateTime::getTimezone() method to get timezone information
The getTimezone() method returns a DateTimeZone object representing the DateTime object’s time zone:
getTimezone(); echo $timezone->getName(); # Prints: Europe/Berlin
DateTimeZone::getLocation
How to get location information for a timezone?
You can get the location information from the time zone object using the getLocation() method. This method returns an associative array that provides the country code of origin of the time zone, longitude & latitude, and some comments.
getLocation(); foreach ($location as $k => $v)< echo "$k: $v
"; > # country_code: IN # latitude: 22.53333 # longitude: 88.36666 # comments:Use the following code if you want to retrieve location information from a DateTime object:
getTimezone(); $location = $timezone->getLocation(); foreach ($location as $k => $v)< echo "$k: $v
"; > # country_code: DE # latitude: 52.5 # longitude: 13.36666 # comments: Germany (most areas)You can also get the name of the timezone by using the getName() method:
getTimezone(); echo $timezone->getName(); # Europe/Berlin
DateTimeZone::getOffset
How to get the timezone offset from UTC (GMT)
The getOffset() method returns the offset from UTC (Universal Coordinated Time) in seconds:
getOffset($date); echo $offset . ' seconds
'; # 19800 seconds echo $offset / 3600 . ' hour(s)'; # 5.5 hour(s)DateTimeZone::getTransitions
How to get all transitions for the timezone?
- ts key: Unix timestamp
- time key: time string
- offset key: UTC offset in seconds
- isdst key: True if daylight saving time is active, false otherwise.
- abbr key: Timezone abbreviation
getTransitions(); print_r($transitions);
The above code prints the following result:
The result enables you to calculate whether daylight saving time is in force at a specific date and time. You can limit the range of results by providing the two optional timestamps: $timestampBegin and $timestampEnd , see the following example:
getTransitions( $timestampBegin, $timestampEnd); print_r($transitions);
The following output prints on the browser:
DateTimeZone::listAbbreviations
How to get DST, UTC offset, and the timezone name.
The listAbbreviations() is a static method, it returns a multidimensional array. The array lists dst (daylight saving time), offset (UTC offset in seconds), and timezone_id for each time zone (e.g. GMT, EDT, EST etc.):
DateTimeZone::listIdentifiers
How to get all defined timezone identifiers?
The listIdentifiers() is a static method, it returns an array of all PHP time zone identifiers.
Africa/Abidjan [1] => Africa/Accra [2] => Africa/Addis_Ababa [3] => Africa/Algiers [4] => Africa/Asmara . . [422] => Pacific/Tongatapu [423] => Pacific/Wake [424] => Pacific/Wallis [425] => UTC )
Get time zone identifiers for a specific region
The listIdentifiers() method takes two arguments:
- $timezoneGroup
This argument accepts one of the DateTimeZone class constants (or a combination) listed below to return time zone identifiers for a specific region. - $countryCode
The second argument accepts a two-letter (uppercase) country code to return a time zone identifier for a specific country. To get an identifier for a specific country you must provide the DateTimeZone::PER_COUNTRY in the first argument.
- DateTimeZone::AFRICA
- DateTimeZone::AMERICA
- DateTimeZone::ANTARCTICA
- DateTimeZone::ARCTIC
- DateTimeZone::ASIA
- DateTimeZone::ATLANTIC
- DateTimeZone::AUSTRALIA
- DateTimeZone::EUROPE
- DateTimeZone::INDIAN
- DateTimeZone::PACIFIC
- DateTimeZone::UTC
- DateTimeZone::ALL
- DateTimeZone::ALL_WITH_BC
- DateTimeZone::PER_COUNTRY
Example: Get time zone identifiers for the Australia
"; > /* Prints: Australia/Adelaide Australia/Brisbane Australia/Broken_Hill Australia/Currie Australia/Darwin Australia/Eucla Australia/Hobart Australia/Lindeman Australia/Lord_Howe Australia/Melbourne Australia/Perth Australia/Sydney */
Get time zone identifier by country
The second argument of listIdentifiers() method accepts a two-letter (uppercase) country code to return a time zone identifier for a specific country. To get an identifier for a specific country you must provide the DateTimeZone::PER_COUNTRY in the first argument:
Example: Get time zone identifiers for USA
"; > /* Prints: America/Adak America/Anchorage America/Boise America/Chicago America/Denver America/Detroit America/Indiana/Indianapolis America/Indiana/Knox America/Indiana/Marengo America/Indiana/Petersburg America/Indiana/Tell_City America/Indiana/Vevay America/Indiana/Vincennes America/Indiana/Winamac America/Juneau America/Kentucky/Louisville America/Kentucky/Monticello America/Los_Angeles America/Menominee America/Metlakatla America/New_York America/Nome America/North_Dakota/Beulah America/North_Dakota/Center America/North_Dakota/New_Salem America/Phoenix America/Sitka America/Yakutat Pacific/Honolulu */
Example: Get time zone identifiers for China
"; > /* Prints: Asia/Shanghai Asia/Urumqi */
The Date and Time Tutorials: