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

PHP Grundanleitung

PHP Fortgeschrittene Anleitung

PHP & MySQL

PHP Referenzhandbuch

Verwendung und Beispiel der Funktion date_default_timezone_set() in PHP

PHP Date & Time Funktionshandbuch

Die Funktion date_default_timezone_set() setzt die Standardzeitzone für alle Datums- und Zeitfunktionen in einem Skript.

Definition und Verwendung

date_default_timezone_set()Die Funktion dient dazu, die Standardzeitzone für alle Funktionen in einem Skript zu setzen.

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

The 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_setUsage of functions-

<?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 the 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 and the time zone set in the ini configuration are different'); 
   } else { 
      print('The script time zone and the time zone set in the ini configuration are the same'); 
   } 
?>
Test and see‹/›

Output result

Default time zone: Asia/Shanghai
The script time zone and the time zone set in the ini configuration are different

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