W3docs

timezone_identifiers_list()

PHP's function date timezone identifiers are used to specify a timezone for a given date or time. These identifiers can be used with the

Introduction

PHP timezone identifiers are strings used to specify a timezone for a given date or time. They follow the Area/Location format (e.g., America/New_York, Europe/London) and are based on the IANA timezone database. These identifiers can be used with the date_default_timezone_set() function to set the default timezone for a script or with the DateTimeZone class to create a new timezone object.

The timezone_identifiers_list() function returns an array of all supported timezone identifiers. This is useful for generating dropdown menus or validating user input.

How to use timezone_identifiers_list() in PHP?

<?php

$identifiers = timezone_identifiers_list();
print_r($identifiers);

The function returns a numerically indexed array containing strings like Africa/Abidjan, America/New_York, Asia/Tokyo, etc. You can iterate over this array to display available timezones:

<?php

foreach (timezone_identifiers_list() as $tz) {
    echo $tz . "\n";
}

To apply one of these identifiers, pass it to date_default_timezone_set() or new DateTimeZone(). For example, to set the timezone to Eastern Standard Time (EST), you would use the following code:

How to use a timezone identifier in PHP?

<?php

date_default_timezone_set('America/New_York');

Alternatively, you can use the DateTimeZone class to create a new timezone object and specify the identifier as a parameter. For example:

How to use the DateTimeZone class in PHP?

<?php

$timezone = new DateTimeZone('Asia/Tokyo');

This creates a new timezone object for the Asia/Tokyo timezone.

It's important to note that some timezone identifiers may not be supported on all systems or may be deprecated. To ensure compatibility and accuracy, we recommend using the timezone database maintained by the Internet Assigned Numbers Authority (IANA).

In addition to the standard timezone identifiers, PHP also supports custom time zones. These can be specified using a UTC offset. For example, to specify a UTC offset of -8 hours, you can use the following code:

Example of PHP custom time zones

<?php

$timezone = new DateTimeZone('-08:00');

This creates a new timezone object with a UTC offset of -8 hours.

Overall, timezone_identifiers_list() provides a powerful and flexible way to manage time zones in your PHP applications. By using the correct identifier for your region, you can ensure accurate and consistent time calculations across all of your systems.

Practice

Practice

Which of the following is a valid timezone identifier in PHP?