English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
PHP Date & Time Function Manual
The date_default_timezone_get() function retrieves the default time zone used by all date-time functions in the script
date_default_timezone_get()The function returns the default time zone used by all date-time functions in the script.
If you use the date_default_timezone_set() function to set the timezone, date_default_timezone_get() returns the previously set timezone value. If you have not explicitly set any default timezone value, this function will return the default timezone value of UTC.
date_default_timezone_get()
The date_default_timezone_get() function does not accept any parameters.
This function returns a string value representing the default timezone.
This function was initially introduced in PHP version5.1is introduced in PHP version 5.2.0 and can be used in all higher versions.
The following example retrieves and prints the current default timezone-
<?php $timeZone = date_default_timezone_get(); print("Default timezone: ".$timeZone); ?>Test and see‹/›
Output result
Default timezone: UTC
In the following example, we usedate_default_timezone_set()function to set the default timezone-
<?php //Set timezone $tz = 'Asia/Shanghai'; date_default_timezone_set($tz); $timeZone = date_default_timezone_get(); print("Default timezone: ".$timeZone); ?>Test and see‹/›
If you usedate_default_timezone_getRetrieve the default timezone, it will return the value you set before-
Default timezone: Asia/Shanghai
The following example prints the default timezone and its abbreviation-
<?php //Set timezone $tz = 'Asia/Kolkata'; date_default_timezone_set($tz); //Retrieve the default timezone $timeZone = date_default_timezone_get(); print("Default timezone: ".$timeZone); print("\n"); //Get abbreviation //$abbvr = $timeZone.date('e').date(T); print("Abbreviation: ". date('T')); ?>Test and see‹/›
Output result
Default timezone: Asia/Kolkata Abbreviation: IST
Get the old timezone and set a new timezone
<?php echo "The old timezone is ". date_default_timezone_get(); $timeZone = 'America/Costa_Rica'; if( date_default_timezone_set( $timeZone) ) { # Now get the timezone. echo "The new timezone is ". date_default_timezone_get(); } ?>Test and see‹/›
Output result:
The old timezone is UTC The new timezone is America/Costa_Rica