Monday, November 16, 2020

PHP | date_default_timezone_get() Function

PHP date_default_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 PHP date_default_timezone_get() Function.

PHP date_default_timezone_get() function returns back the default timezone that is used by all the functions in a script.

If you set the timezone using the date_default_timezone_set(), then the date_default_timezone_get() returns the time zone value which was previously set. If you have not explicitly set any default time zone value then this function will return the default timezone value of UTC.

Syntax

Following below is the syntax to use this function -

date_default_timezone_get()


Parameter Details

This function does not accept any parameters.

Return Value

This PHP function returns a string value representing the default time zone.

PHP Version

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

Example1

Following example retrieves and prints the current default timezone to the screen -

<?php
   $timeZone = date_default_timezone_get();
   print("Default timezone: ".$timeZone);
?>

Output

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

Default timezone: UTC

Example2

In the following example below, we are setting the default time zone using date_default_timezone_set() function -

<?php
   //setting the timezone
   $tz = 'Nigeria/Rivers';   
   date_default_timezone_set($tz);
   $timeZone = date_default_timezone_get();
   print("Default timezone: ".$timeZone);
?>

Output

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

Default timezone: Nigeria/Rivers

Example3

The following example prints the default timezone alongside with its abbreviation -

<?php
   //setting the timezone
   $tz = 'Eastern Africa Time';   
   date_default_timezone_set($tz);
   //Retrieving the default timezone
   $timeZone = date_default_timezone_get();
   print("Default timezone: ".$timeZone);
   print("\n");

   //Getting abbreviation
   //$abbvr = $timeZone.date('e').date(T);
   print("Abbreviation: " .date('T'));
?>

Output

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

Default timezone: Eastern Africa Time
Abbreviation: EAT

Example4

Try the following example below -

<?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 PHP date_default_timezone_set() Function.

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.