PHP | date_default_timezone_get() Function

The date_default_timezone_get() function is an inbuilt function in PHP which is used to gets the default timezone used by all date/time functions in a script.

Syntax:

string date_default_timezone_get( void )

Parameter: This function does not accepts any parameter.



Return Value: This function returns a string.

Note: This function returns the default timezone by:

  • Reading the timezone using the date_default_timezone_set() function.
  • Before PHP 5.4.0, Reading the TZ environment variable.
  • Reading the value of the date.timezone ini option
  • Before PHP 5.4.0, Querying the host operating system (if supported and allowed by the OS). This uses an algorithm that has to guess the timezone.

If any statement of above not true then date_default_timezone_get() will return a default timezone of UTC.

Below programs illustrate the date_default_timezone_get() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Set the default timezone
date_default_timezone_set('Asia/Kolkata');
  
// Create timezone object
$timezone_object = date_default_timezone_get();
  
// If timzone object is true 
if ($timezone_object) {
    echo 'date_default_timezone_set: ' . date_default_timezone_get();
}
?>

chevron_right


Output:

date_default_timezone_set: Asia/Kolkata

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Set the default timezone
date_default_timezone_set('Asia/Kolkata');
  
echo date_default_timezone_get() . ' => ' . date('e') . ' => ' . date('T');
?>

chevron_right


Output:

Asia/Kolkata => Asia/Kolkata => IST

Related Articles:

Reference: http://php.net/manual/en/function.date-default-timezone-get.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.