English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP date_default_timezone_set() Function Usage and Examples

PHP Date & Time Function Manual

The date_default_timezone_set() function sets the default time zone for all date and time functions in a script

Definition and Usage

date_default_timezone_set()The function is used to set the default time zone for all functions in the script.

Syntax

date_default_timezone_set(timezone)

Parameter

Serial numberParameters and descriptions
1

timezone (required)

The string that needs to be set as the default time zone.

Return value

PHP date_default_timezone_set() function returns a boolean value, if the given time zone string is valid, thentrue,otherwisefalse.

PHP version

This function was originally introduced in PHP version5.1introduced in version 5.2.0 and can be used in all higher versions.

Online example

The following example demonstratesdate_default_timezone_setfunction usage-

<?php
   //Set time zone
   $tz = 'Asia/Shanghai';   
   date_default_timezone_set($tz);
   $timeZone = date_default_timezone_get();
   print('Default time zone: '. $timeZone);
?>
Test and see‹/›

Output result

Default time zone: Asia/Shanghai

Online example

The following example compares the default time zone and ini-Set time zone.-

<?php
   //Set time zone
   $tz = 'Asia/Shanghai';   
   date_default_timezone_set($tz);
   //Retrieve the default time zone
   $timeZone = date_default_timezone_get();
   print('Default time zone: '. $timeZone);
   print('\n');
   //Compare the time zone with the time zone set in the ini configuration
   if (strcmp($timeZone, ini_get('date.timezone'))){ 
      print('The script time zone is different from the ini configuration set time zone'); 
   } else { 
      print('The script time zone is the same as the ini configuration set time zone'); 
   } 
?>
Test and see‹/›

Output result

Default time zone: Asia/Shanghai
The script time zone is different from the ini configuration set time zone

Online example

<?php
$dateSrc = '2007-04-19 12:50 GMT';
$dateTime = date_create($dateSrc);;
$DateTimeZone = date_timezone_get($dateTime);
   
echo 'Return time zone is '. timezone_name_get($DateTimeZone);
echo '\n';
# Use the second function.
$dateTime = new DateTime($dateSrc);
$DateTimeZone = $dateTime;-getTimezone();
   
echo 'Return time zone is '. timezone_name_get($DateTimeZone);
?>
Test and see‹/›

Output result:

Return time zone is GMT
Return time zone is GMT