Friday, September 5, 2025
HomeLanguagesPHP | timezone_open() Function

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:




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


Output:

The new DateTimeZone object created is America/Chicago

Program 2:




<?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>");
}
?>


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

RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6636 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11865 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7026 POSTS0 COMMENTS
Thapelo Manthata
6703 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS