Date/Time 函数
在线手册:中文 英文
PHP手册

date_default_timezone_get

(PHP 5 >= 5.1.0)

date_default_timezone_get取得一个脚本中所有日期时间函数所使用的默认时区

说明

string date_default_timezone_get ( void )

本函数返回默认时区,使用如下“假定”的顺序:

返回值

返回一个 string

更新日志

版本 说明
5.4.0 The TZ environment variable is no longer used to guess the timezone.
5.4.0 The timezone is no longer guessed from information available through the operating system as the guessed timezone can not be relied on.

范例

Example #1 Getting the default timezone

<?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 Getting the abbreviation of a timezone

<?php
date_default_timezone_set
('America/Los_Angeles');
echo 
date_default_timezone_get() . ' => ' date('e') . ' => ' date('T');
?>

以上例程会输出:

America/Los_Angeles => America/Los_Angeles => PST

参见


Date/Time 函数
在线手册:中文 英文
PHP手册
PHP手册 - N: 取得一个脚本中所有日期时间函数所使用的默认时区

用户评论:

glennpratt+php at gmail dot com (19-May-2011 09:30)

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());
?>

harmor (28-Feb-2008 04:19)

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

dohpaz at gmail dot com (20-Apr-2007 10:58)

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.