• PHP Video Tutorials

PHP timezone_open() Function


Advertisements

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)

Parameters

Sr.No Parameter & Description
1

timezone (Mandatory)

This is a string value representing a timezone.

Return Values

PHP timezone_name_get() function returns DateTimeZone object. Incase of failure this function returns the boolean value false.

PHP Version

This function was first introduced in PHP Version 5.2.0 and, works with all the later versions.

Example

Following example demonstrates the usage of the timezone_open() function −

Live Demo
<?php
   $tz = "Indian/mahe";
   $res = timezone_open($tz);   
   print_r($res);
?>

This will produce following result −

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

Example

$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 'New timeZone is '. timezone_name_get($NewDateTimeZone);
echo "\n";

# Using second function.
$dateTime = new DateTime($dateSrc);
   
$DateTimeZone = new DateTimeZone( 'America/Chicago' );
$dateTime->setTimezone( $DateTimeZone );
$NewDateTimeZone = $dateTime->getTimezone ();
   
echo 'New timeZone is '. timezone_name_get ($NewDateTimeZone);

This will produce the following result −

New timeZone is America/Chicago
New timeZone is America/Chicago
php_function_reference.htm
Advertisements