Tuesday, November 17, 2020

PHP | date_default_timezone_set() Function

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

PHP date_default_timezone_set() function is used to set 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.NoParameter & 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

Below 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 rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP date_format() 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.