English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP timezone_open() Function Usage and Examples

PHP Date & Time Functions Manual

The timezone_open() function creates a new DateTimeZone object

Definition and Usage

The timezone_open() function is an alias of DateTimeZone::__construct(). It accepts a timezone string as a parameter and creates a DateTimeZone object.

Syntax

timezone_open($timezone)

Parameter

Serial numberParameters and descriptions
1

timezone (required)

This is a string value representing the timezone.

Return value

The timezone_name_get() function returns a DateTimeZone object. If it fails, this function returns a boolean valuefalse.

PHP version

This function was originally introduced in PHP version5.2introduced in version 5.2.0 and can be used in all higher versions.

Online example

The following examples demonstratetimezone_open()Function usage-

<?php
   $tz = "Indian/mahe";
   $res = timezone_open($tz);   
   print_r($res);
?>
Test and see‹/›

Output result

DateTimeZone Object
(
  [timezone_type] => 3
  [timezone] => Indian/mahe
)

Online example

Create a new DateTimeZone object and then return the timezone name:

<?php
$dateSrc = '2017-06-25 1:50 GMT';
$dateTime = date_create($dateSrc);
   
$DateTimeZone = timezone_open('America';/Chicago');
date_timezone_set($dateTime, $DateTimeZone);
$NewDateTimeZone = date_timezone_get($dateTime);
   
echo 'The new timezone is '. timezone_name_get($NewDateTimeZone);
echo "\n";
# Use the second method
$dateTime = new DateTime($dateSrc);
   
$DateTimeZone = new DateTimeZone('America';/Chicago');
$dateTime->setTimezone($DateTimeZone);
$NewDateTimeZone = $dateTime;->getTimezone();
   
echo 'The new timezone is '. timezone_name_get($NewDateTimeZone);
?>
Test and see‹/›

Output result:

The new timezone is America/Chicago
The new timezone is America/Chicago