PHP Timezone

by Vincy. Last modified on July 3rd, 2022.

In PHP, working with timezone is quite interesting by utilizing various functions coming under PHP date/time. In this article, we are going to cover the following list of items.

  • How to get a list of PHP-supported timezone identifiers.
  • PHP timezone getters and setters.
  • PHP timezone conversion.

How to Get the list of PHP Supported Timezone Identifiers

As of PHP version 5.2, it introduces DateTimeZone class in which the function named as listIdentifiers() is defined. This function will return a list of all identifiers as an associative array of strings.

This function accepts two optional arguments, like,

  • DateTimeZone constants – This argument is used to return an array of timezone identifiers with respect to the continent, sub-continent, country and etc. If it is specified as ALL or if this is empty, then all available identifiers will be returned.
  • Country Code – The standard two-character code is used to represent the country for this second argument. And it requires the first argument should be PER_COUNTRY.

We can invoke this function with the references of the class name by using the PHP scope resolution operator as shown below.

$timezoneIderntifiers = DateTimeZone::listIdentifiers();
print "<pre>";
print "</pre>";

Since DateTimeZone::listIdentifiers() doesn’t have an argument, the above program will return all available time zone to be displayed to the browser.

In PHP, there is an alias of this DateTimeZone class function, which is named, timezone_identifiers_list(). But, since it is recommended to use the upgraded code as we have seen in the article PHP Array Length, we can prefer DateTimeZone class function shown in the example above.

PHP Timezone Getter and Setter

PHP includes two functions named date_default_timezone_get() and date_default_timezone_set(), as timezone getter and setter respectively.

  • date_default_timezone_get() – This function will return the timezone  set using PHP timezone setter, or a value specified with php.ini date.timezone directive if any. (PHP version below 5.4, this function can take the value of $_ENV[“TZ”], if available.).
  • date_default_timezone_set() – This function accepts the PHP supported format of timezone string identifier as its argument.

In the following program, the PHP timezone getter returns the value of the php.ini option until it is overridden by the setter function as shown below.

$timezone_identifier = date_default_timezone_get();
echo $timezone_identifier . "<br/>";
$timezone_identifier = date_default_timezone_get();
echo $timezone_identifier;

Other PHP Functions to Get/Set Timezone

  • date(‘e’) / date(‘T’) –  date() function is used to get timezone identifier by passing ‘e’ as its argument. When we pass ‘T’ to this function, then it will return an abbreviated time zone.
  • setTimezone() – This function is used to set timezone upon a DateTime object by the given timezone identifier as its argument.

PHP Timezone Conversion

In PHP, there is no inbuilt function to convert from one time zone to another. Rather, we can obtain this conversion process by creating a DateTimeZone instance for the required timezone and this instance should be applied to the date-time object involved in timezone conversion. For example,

$objDateTime = new DateTime("1978-11-21 12:12:12");
$timezone_array["asian_time"] = $objDateTime->format('d-m-Y H:i:s');
$objDateTimeZone = new DateTimeZone('America/Los_Angeles');
$timezone_array["us_time"] = $objDateTime->format('d-m-Y H:i:s');
print "<pre>";
print "</pre>";
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page