| by Arround The Web | No comments

How to Fetch PHP Timezone List

While developing a web application or working with date or time-related data in PHP, setting the correct timezone is crucial to ensure that the application works accurately and consistently across different regions. PHP provides a comprehensive list of timezones that we can use, but fetching the list can be daunting for beginners.

This guide covers how one can fetch the PHP timezone list.

How to Fetch PHP Timezone List

In PHP timezone list can be fetched using:

  • Timezone_identifiers_list Function
  • DateTimeZone::listIdentifiers function

The above functions can be used in different ways depending on user requirements. Here, we will show different ways to use these functions to fetch:

1: Fetch All Timezones in PHP

To fetch all timezones in PHP through the Timezone_identifiers_list function, follow the below-given code:

<?php

$timezones = timezone_identifiers_list();

print_r($timezones);

?>

To retrieve all the timezones in PHP using the DateTimeZone::listIdentifiers function, use the following code:

<?php

var_dump(

    DateTimeZone::listIdentifiers(

            DateTimeZone::ALL

    )

);

?>

2: Fetch Timezone Based on Continent in PHP

If you want to fetch the timezone of the specific continent using the timezone_identifiers_list function, you can follow the below-given code:

<?php

$specific_timezones= timezone_identifiers_list(DateTimeZone::AMERICA);

print_r($specific_timezones);

?>

In case of fetching the timezone of the specific continent using the DateTimeZone::listIdentifiers function, you can follow the below-given example:

<?php

var_dump(

    DateTimeZone::listIdentifiers(

            DateTimeZone::AMERICA

    )

);

?>

Note: Make sure to type the name of the continent in uppercase.

3: Fetch Timezone Based on Country in PHP

To fetch the timezone of a specific country using the timezone_identifiers_list function, you can follow the below-given PHP code:

<?php

$specific_timezones = timezone_identifiers_list(DateTimeZone::PER_COUNTRY, 'US');

print_r($specific_timezones);

?>

Note: In place of US, you can add your country code.

To fetch the timezone of a specific country using the DateTimeZone::listIdentifiers function, use the following code:

<?php

$country_timezones = DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, 'US');

print_r($country_timezones);

?>

Bottom Line

Setting the correct timezone in PHP is essential for accurate and consistent date and time-related data across different regions. PHP provides a comprehensive list of timezones that can be fetched using the timezone_identifiers_list() or DateTimeZone::listIdentifiers() functions. By following the examples provided in this guide, developers can fetch all timezones, timezones based on the continent, and timezones based on country.

Share Button

Source: linuxhint.com

Leave a Reply