PHP | date_timezone_get() Function

PHP date_timezone_get() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP date_timezone_get() Function.

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

Syntax

Following below is the syntax to use this function -

date_timezone_get($object)


Parameter Details

Sr.NoParameter & Description
1

object (Mandatory)

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


Return Value

This PHP function returns a DateTimeZone object. In case of failure, this PHP function returns the boolean value FALSE.

PHP Version

This function was first introduced as part of core PHP v 5.2.1 and, it works with all the later versions.

Example1

Following example illustrates the usage of the PHP date_timezone_get() function -

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

Output

When the above code is executed, it will produce the following result -

Timezone: UTC

Example2

The following example below sets a time zone and retrieves it back using the PHP date_timezone_get() function -

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

Output

When the above code is executed, it will produce the following result -

DateTimeZone Object
(
    [timezone_type] => 3
    [timezone] => Nigeria/Rivers
)

Example3

Try the following example -

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

Output

When the above code is executed, it will produce the following result -

Default timezone: Nigeria/Rivers

Example4

Try the following 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();
   }
?>

Output

When the above code is executed, it will produce the following result -

Old time zone is America/Denver
New time zone is America/Costa_Rica


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the date_timezone_set() Function in PHP.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.