PHP | timezone_open() Function

The timezone_open() function is an inbuilt function in PHP which is used to create a new DateTimeZone object. The timezone_open() function accepts the timezone as a parameter and returns the DateTimeZone object on success or False on failure.

Syntax:

timezone_open( $timezone )

Parameters: This function accepts single parameter $timezone which is mandatory. It specify the timezone of the new DateTimeZone object to be created.

Return Value: It returns the DateTimeZone object on success or False on failure.

Exceptions: The timezone passed as a parameter must be a supported timezone in PHP else it may result in incorrect results.

Below programs illustrate the timezone_open() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Creating a new DateTimeZone object
$timezone = timezone_open("America/Chicago");
  
echo ("The new DateTimeZone object created is " 
              . timezone_name_get($timezone ));
?>

chevron_right


Output:

The new DateTimeZone object created is America/Chicago

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Array of timezones
$timezones = array('Europe/London', 'Asia/Kolkata');
foreach ($timezones as $tz) {
    $name = timezone_open($tz);
      
    echo ("The new DateTimeZone object created is "
                . timezone_name_get($name). "<br>");
}
?>

chevron_right


Output:

The new DateTimeZone object created is Europe/London
The new DateTimeZone object created is Asia/Kolkata

Note: The timezone_open() function gives warning since the timezone passed is not a supported/valid timezone.

Reference: http://php.net/manual/en/function.timezone-open.php



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.