Thursday, November 19, 2020

PHP | date_timezone_get() Function

PHP date_timezone_get() Function


Hello dear readers! welcome back to another edition of our tutorial on PHP. In this tutorial guide, we are going to be discussing about the date_timezone_get() Function in PHP.

PHP date_timezone_get() function is an alias of the built-in PHP DateTime::getTimezone() function. This function accepts a DateTime object as parameter and 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 built-in PHP function returns a DateTimeZone object. Incase of failure, it 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 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 sets a time zone and retrieves it back using the 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 rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the date_timezone_set() Function in PHP.

Do feel free to ask your questions where necessary and i 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.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.