PHP Function Reference

PHP timezone_open() Function



The PHP timezone_open() function returns a new DateTimeZone object. This function is an alias of DateTimeZone::__construct() method.

Syntax

//Object-oriented style
public DateTimeZone::__construct(timezone)

//Procedural style
timezone_open(timezone)

Parameters

timezone Required. Specify a string value representing one of the supported timezones.

Return Value

Returns a new DateTimeZone object on success. Procedural style returns false on failure.

Exceptions

Throws Exception if the timezone supplied is not recognized as a valid timezone.

Example: using both styles

The example below shows the usage of timezone_open() function.

<?php
  //creating a DateTimeZone object 
  //using object-oriented style
  $tz1 = new DateTimeZone("Europe/London");

  //displaying the DateTimeZone
  print_r($tz1);

  echo "\n";

  //creating a DateTimeZone object 
  //using procedural style
  $tz2 = timezone_open("America/Chicago");

  //displaying the DateTimeZone
  print_r($tz2);
?>

The output of the above code will be:

DateTimeZone Object
(
    [timezone_type] => 3
    [timezone] => Europe/London
)

DateTimeZone Object
(
    [timezone_type] => 3
    [timezone] => America/Chicago
)

Example: using DateTimeZone object

The DateTimeZone object can be used to create a DateTime object with given timezone or modify the timezone of a given DateTime object. Consider the example below:

<?php
  //creating a DateTimeZone object 
  $tz1 = timezone_open("Europe/London");

  //creating a DateTime object
  $date = date_create("10-May-2015", $tz1);

  //getting the current time zone
  $curr_tz = date_timezone_get($date);
  echo "Current TimeZone: ".timezone_name_get($curr_tz)."\n";  
 
  //creating another DateTimeZone object 
  $tz2 = timezone_open("America/Chicago");

  //setting the timezone of a date to new value
  date_timezone_set($date, $tz2);

  //getting the modified time zone
  $mod_tz = date_timezone_get($date);
  echo "Modified TimeZone: ".timezone_name_get($mod_tz)."\n";  
?>

The output of the above code will be:

Current TimeZone: Europe/London
Modified TimeZone: America/Chicago

❮ PHP Date and Time Reference