• PHP Video Tutorials

PHP date_timezone_get() Function


Advertisements

Definition and Usage

The date_timezone_get() function is an alias of DateTime::getTimezone. It accepts a DateTime object as a parameter and returns the timezone object relative to the given date/time (object).

Syntax

date_timezone_get($object)

Parameters

Sr.No Parameter & Description
1

object (Mandatory)

This represents the DateTime object for which you need the timezone.

Return Values

This function returns a DateTimeZone object. Incase of failure it returns the boolean value false.

PHP Version

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

Example

Following example of the date_timezone_get() function −

Live Demo
<?php
   $date = date_create("25-09-1989");   
   $res = date_timezone_get($date);
   $timeZone_name = timezone_name_get($res);
   print("Timezone: ".$timeZone_name);
?>

This will produce following result −

Timezone: UTC

Example

Following example sets a time zone and retrieve it back using the date_timezone_get() function.−

<?php
   $tz = new DateTimeZone("Indian/Mahe"); 
   $date = date_create("25-09-1989", $tz);   
   $res = date_timezone_get($date);
   print_r($res);
?>

This will produce following result −

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

Example

The date_timezone_get() function just gives you the timezone object you can get its name using timezone_name_get()

Live Demo
<?php
   $tz = new DateTimeZone("Indian/Mahe"); 
   $date = date_create("25-09-1989", $tz);   
   $res = date_timezone_get($date);
   $timeZone_name = timezone_name_get($res);
   print("Timezone: ".$timeZone_name);
?>

This will produce following result −

Default timezone: Indian/Mahe

Example

<?php
   echo "Old time zone is ". date_default_timezone_get();
   $timeZone = 'America/Costa_Rica';
   
   if( date_default_timezone_set( $timeZone) ){
      # Now get this time zone.
      echo "New time zone is ". date_default_timezone_get();
   }
?>

This will produce the following result −

Old time zone is America/Denver
New time zone is America/Costa_Rica
php_function_reference.htm
Advertisements