Get a List of All Available Time Zones
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) {
echo $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) {
echo $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) {
echo $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) {
echo $timeZone;
}
This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.