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

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP date_timezone_get() Function Usage and Example

PHP Date & Time Functions Manual

The date_timezone_get() function returns the timezone relative to the given DateTime

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 relative to the given date./The timezone object of the time (object).

Syntax

date_timezone_get($object)

Parameter

NumberParameters and Description
1

object (required)

This indicates that you need the DateTime object for the timezone.

Return value

This function returns a DateTimeZone object. If it fails, it returns a boolean valuefalse.

PHP version

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

Online Example

The following isdate_timezone_get()Example of the function-

<?php
   $date = date_create("25-09-1989");   
   $res = date_timezone_get($date);
   $timeZone_name = timezone_name_get($res);
   print("Timezone: ". $timeZone_name);
?>
Test and see‹/›

Output result

Timezone: UTC

Online Example

The following example sets the timezone and usesdate_timezone_get()function retrieves it back.

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

Output result

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

Online Example

Thisdate_timezone_get()function just gives you the timezone object, you can get its nameUse timezone_name_get()   -

<?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("Default timezone: ". $timeZone_name);
?>
Test and see‹/›

Output result

Default timezone: Indian/Mahe

Online Example

Change the default timezone

<?php
   echo "The old timezone is ". date_default_timezone_get();
   $timeZone = 'Asia/Shanghai';
   
   if(date_default_timezone_set($timeZone)){
      #Now we get this timezone.
      echo "The new timezone is ". date_default_timezone_get();
   }
?>
Test and see‹/›

Output result:

The old timezone is UTC
The new timezone is Asia/Shanghai

PHP Date & Time Functions Manual