PHP Timezone

June 27, 2013

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 list of PHP supported timezone identifiers.
  • PHP timezone getters and setters.
  • PHP timezone conversion.

php_time_zone

How to Get 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 list of all identifiers as an associative array of string.

This function accepts two optional arguments, like,

  • DateTimeZone constants – This argument is used to return array of timezone identifiers with respect to 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 country for this second argument. And it requires the first argument should be PER_COUNTRY.

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

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

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

In PHP, there is an alias of this DateTimeZone class function, which is named as, timezone_identifiers_list(). But, since it is recommended to use 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 timzone  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 PHP timezone getter returns the value of php.in option until it is overridden by the setter function as shown below.

$timezone_identifier = date_default_timezone_get();
echo $timezone_identifier . "<br/>";
date_default_timezone_set('Asia/Kolkata');
$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 abbreviated time zone.
  • setTimezone() – This function is used to set timezone upon a datetime object by the give timezone identifier as its argument.

PHP Timezone Conversion

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

date_default_timezone_set('Asia/Kolkata');
$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');
$objDateTime->setTimeZone($objDateTimeZone);
$timezone_array["us_time"] = $objDateTime->format('d-m-Y H:i:s');
print "<pre>";
print_r($timezone_array);
print "</pre>";

«

»