(PHP 5 >= 5.1.0, PHP 7, PHP 8)
date_default_timezone_get — 取得一個(gè)腳本中所有日期時(shí)間函數所使用的默認時(shí)區
本函數返回默認時(shí)區,使用如下“假定”的順序:
用 date_default_timezone_set() 函數設定的時(shí)區(如果設定了的話(huà))
僅僅在 PHP 5.4.0 之前: TZ 環(huán)境變量(如果非空)
date.timezone 配置選項(如果設定了的話(huà))
僅僅在 PHP 5.4.0 之前: 查詢(xún)操作系統主機 (如果操作系統支持并允許)。 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
的默認時(shí)區。
返回一個(gè) string。
版本 | 說(shuō)明 |
---|---|
5.4.0 |
不再使用 TZ 來(lái)推測時(shí)區。
|
5.4.0 | 不再根據操作系統的信息來(lái)推測時(shí)區,因為這是不可靠的。 |
示例 #1 獲取默認時(shí)區
<?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');
}
?>
以上例程的輸出類(lèi)似于:
date_default_timezone_set: Europe/London date.timezone: Europe/London
示例 #2 獲取一個(gè)時(shí)區的簡(jiǎn)寫(xiě)
<?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