date_default_timezone_get

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

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

說明

date_default_timezone_get(): string

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

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

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

  • date.timezone 配置選項(如果設定了的話)

  • 僅僅在 PHP 5.4.0 之前: 查詢操作系統(tǒng)主機 (如果操作系統(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() 會則返回 UTC 的默認時區(qū)。

返回值

返回一個 string

更新日志

版本 說明
5.4.0 不再使用 TZ 來推測時區(qū)。
5.4.0 不再根據(jù)操作系統(tǒng)的信息來推測時區(qū),因為這是不可靠的。

范例

示例 #1 獲取默認時區(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 獲取一個時區(qū)的簡寫

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

以上例程會輸出:

America/Los_Angeles => America/Los_Angeles => PST

參見