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_set() Function.
The date_default_timezone_set() function in PHP is used in setting the default timezone used by all the functions in the script.
The date_default_timezone_set() function in PHP is used in setting the default timezone used by all the functions in the script.
Syntax
Following below is the syntax to use this function -
date_default_timezone_set(timezone)
Parameter Details
Sr.No | Parameter & Description |
---|---|
1 | timezone (Mandatory) This is the string representing the time zone you need to set as default. |
Return Value
This PHP function returns true if the given timezone string is valid and, false if it is not valid.
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 shows the usage of the 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
Example2
Following example compares the default time zone and the ini-set timezone -
<?php //setting the timezone $tz = 'Eastern African Time'; date_default_timezone_set($tz); //Retrieving the default timezone $timeZone = date_default_timezone_get(); print("Default timezone: ".$timeZone); print("\n"); //Comparing the timezone with ini-set timezone if (strcmp($timezone, ini_get('date.timezone'))){ print('Script timezone and ini-set timezone are not same.'); } else { print('Script timezone and ini-set timezone are same.'); } ?>
Output
When the above code is executed, it will produce the following result -
Array Default timezone: Eastern African Time Script timezone and ini-set timezone are same.
Example3
Try the following example below -
$dateSrc = '2007-04-19 12:50 GMT'; $dateTime = date_create( $dateSrc);; $DateTimeZone = date_timezone_get ( $dateTime ); echo 'Return timeZone is '. timezone_name_get ($DateTimeZone); echo "\n"; # Using second function. $dateTime = new DateTime($dateSrc); $DateTimeZone = $dateTime->getTimezone (); echo 'Return timeZone is '. timezone_name_get ($DateTimeZone);
Output
When the above code is executed, it will produce the following result -
Return timeZone is America/Denver Return timeZone is America/Denver
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_format() 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.