Get "PHP 8 in a Nuthshell" (Now with PHP 8.4)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

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.
  • DateTimeZone::AMERICA
    • America time zones.
  • DateTimeZone::ANTARCTICA
    • Antarctica time zones.
  • DateTimeZone::ARCTIC
    • Arctic time zones.
  • DateTimeZone::ASIA
    • Asia time zones.
  • DateTimeZone::ATLANTIC
    • Atlantic time zones.
  • DateTimeZone::AUSTRALIA
    • Australia time zones.
  • DateTimeZone::EUROPE
    • Europe time zones.
  • DateTimeZone::INDIAN
    • Indian time zones.
  • DateTimeZone::PACIFIC
    • Pacific time zones.
  • DateTimeZone::UTC
    • UTC time zones.
  • DateTimeZone::ALL
    • All time zones.
  • DateTimeZone::ALL_WITH_BC
    • All time zones including backwards compatible.
  • DateTimeZone::PER_COUNTRY
    • Time zones per country.
Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), 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.

Like this article?

Buy me a coffee

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?