©
This document uses PHP Chinese website manual Release
(PHP 5 >= 5.1.0)
date_default_timezone_get — 取得一个脚本中所有日期时间函数所使用的默认时区
本函数返回默认时区,使用如下“假定”的顺序:
用 date_default_timezone_set() 函数设定的时区(如果设定了的话)
仅仅在 PHP 5.4.0 之前: TZ 环境变量(如果非空)
date.timezone 配置选项(如果设定了的话)
仅仅在 PHP 5.4.0 之前: 查询操作系统主机 (如果操作系统支持并允许)。 This uses an algorithm that has to guess the timezone. This is by no means going to work correctly for every situation. A warning is shown when this stage is reached. Do not rely on it to be guessed correctly, and set date.timezone to the correct timezone instead.
如果以上选择都不成功, date_default_timezone_get() 会则返回 UTC 的默认时区。
返回一个 string 。
版本 | 说明 |
---|---|
5.4.0 | 不再使用 TZ 来推测时区。 |
5.4.0 | 不再根据操作系统的信息来推测时区,因为这是不可靠的。 |
Example #1 获取默认时区
<?php
date_default_timezone_set ( 'Europe/London' );
if ( date_default_timezone_get ()) {
echo 'date_default_timezone_set: ' . date_default_timezone_get () . '<br />' ;
}
if ( ini_get ( 'date.timezone' )) {
echo 'date.timezone: ' . ini_get ( 'date.timezone' );
}
?>
以上例程的输出类似于:
date_default_timezone_set: Europe/London date.timezone: Europe/London
Example #2 获取一个时区的简写
<?php
date_default_timezone_set ( 'America/Los_Angeles' );
echo date_default_timezone_get () . ' => ' . date ( 'e' ) . ' => ' . date ( 'T' );
?>
以上例程会输出:
America/Los_Angeles => America/Los_Angeles => PST
[#1] andrea at -REMOVE- bhweb dot it [2015-07-22 17:21:12]
Please note that "Damien dot Garrido dot Work at gmail dot com" code is wrong, the third parameter of sprintf must be divided by 60.
This is the corrected function:
<?php
function timezone_offset_string( $offset )
{
return sprintf( "%s%02d:%02d", ( $offset >= 0 ) ? '+' : '-', abs( $offset / 3600 ), abs( $offset % 3600 ) / 60 );
}
?>
[#2] bohwaz [2013-09-12 03:23:21]
Please note that on Debian/Ubuntu this function will return the system timezone defined in /etc/localtime if date.timezone is not defined, even with PHP 5.4+
See http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=673763#10
[#3] benrwhite at gmail dot com [2013-07-12 11:31:52]
For the reason that date_default_timezone_get() throws an error when the timezone isn't set in php.ini and then returns a default chosen by the system (rather than returning false to indicate to the script that a timezone hasn't been set), I've found that the following works when you want a script to detect when the ini value has not been set and want the script itself to choose a default in that case, while still allowing bootstrap scripts to set their own default using date_default_timezone_set().
<?php
set_error_handler(function ($errno, $errstr){
throw new Exception($errstr);
return false;
});
try{
date_default_timezone_get();
}
catch(Exception $e){
date_default_timezone_set('UTC'); // Sets to UTC if not specified anywhere in .ini
}
restore_error_handler();
[#4] Damien dot Garrido dot Work at gmail dot com [2012-07-05 00:04:05]
To get offset string from offset:
<?php
function timezone_offset_string( $offset )
{
return sprintf( "%s%02d:%02d", ( $offset >= 0 ) ? '+' : '-', abs( $offset / 3600 ), abs( $offset % 3600 ) );
}
$offset = timezone_offset_get( new DateTimeZone( 'Pacific/Kiritimati' ), new DateTime() );
echo "offset: " . timezone_offset_string( $offset ) . "\n";
$offset = timezone_offset_get( new DateTimeZone( 'Pacific/Tahiti' ), new DateTime() );
echo "offset: " . timezone_offset_string( $offset ) . "\n";
?>
Output:
offset: +14:00
offset: -10:00
[#5] glennpratt+php at gmail dot com [2011-05-19 13:30:45]
In my case, I'm not sure I can guess the correct timezone any better than PHP and it's no where near important enough to nag the user, so...
<?php
// Suppress DateTime warnings
date_default_timezone_set(@date_default_timezone_get());
?>
[#6] harmor [2008-02-28 08:19:57]
If you want to get the abbrivation (3 or 4 letter), instead of the long timezone string you can use date('T') function like this:
Input:
date_default_timezone_set('America/Los_Angeles');
echo date_default_timezone_get();
echo ' => '.date('e');
echo ' => '.date('T');
Output:
America/Los_Angeles => America/Los_Angeles => PST
[#7] dohpaz at gmail dot com [2007-04-20 14:58:32]
date_default_timezone_get() will still emit a warning in E_STRICT if the timezone is not set; either by date_default_timezone_set() or the ini option of date.timezone.
This is probably not a big deal, but I thought I would contribute what I found.