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_default_timezone_get() Function.
PHP date_default_timezone_get() function return the default timezone used by all the functions in a script.
If you set the timezone using the built-in PHP 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.
PHP date_default_timezone_get() function return the default timezone used by all the functions in a script.
If you set the timezone using the built-in PHP 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 built-in PHP function does not accept any parameters.
Return Value
This built-in 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, we are setting the default time zone making use of the built-in PHP 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 going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the PHP date_default_timezone_set() Function.
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.
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.