date_default_timezone_get

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

date_default_timezone_get取得一個(gè)腳本中所有日期時(shí)間函數(shù)所使用的默認(rèn)時(shí)區(qū)

說(shuō)明

date_default_timezone_get(): string

本函數(shù)返回默認(rèn)時(shí)區(qū),使用如下“假定”的順序:

  • date_default_timezone_set() 函數(shù)設(shè)定的時(shí)區(qū)(如果設(shè)定了的話)

  • 僅僅在 PHP 5.4.0 之前: TZ 環(huán)境變量(如果非空)

  • date.timezone 配置選項(xiàng)(如果設(shè)定了的話)

  • 僅僅在 PHP 5.4.0 之前: 查詢操作系統(tǒng)主機(jī) (如果操作系統(tǒng)支持并允許)。 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() 會(huì)則返回 UTC 的默認(rèn)時(shí)區(qū)。

返回值

返回一個(gè) string

更新日志

版本 說(shuō)明
5.4.0 不再使用 TZ 來(lái)推測(cè)時(shí)區(qū)。
5.4.0 不再根據(jù)操作系統(tǒng)的信息來(lái)推測(cè)時(shí)區(qū),因?yàn)檫@是不可靠的。

范例

示例 #1 獲取默認(rèn)時(shí)區(qū)

<?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

示例 #2 獲取一個(gè)時(shí)區(qū)的簡(jiǎn)寫

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

以上例程會(huì)輸出:

America/Los_Angeles => America/Los_Angeles => PST

參見