查詢

IntlTimeZone::createTimeZone()函式—用法及示例

「 建立一個新的 IntlTimeZone 物件,該物件表示給定的時區識別符號 」


函式名稱:IntlTimeZone::createTimeZone()

適用版本:PHP 5 >= 5.5.0, PHP 7, PHP 8

用法:IntlTimeZone::createTimeZone() 函式用於建立一個新的 IntlTimeZone 物件,該物件表示給定的時區識別符號。

語法:public static IntlTimeZone::createTimeZone(string $timezoneId) : IntlTimeZone|false

引數:

  • $timezoneId:要建立的時區的識別符號,例如 "Asia/Shanghai"。

返回值:

  • 如果時區識別符號有效,則返回一個新的 IntlTimeZone 物件。
  • 如果時區識別符號無效,則返回 false。

示例:

// 建立一個表示 "Asia/Shanghai" 的 IntlTimeZone 物件
$timezone = IntlTimeZone::createTimeZone("Asia/Shanghai");
if ($timezone) {
    echo $timezone->getDisplayName() . "\n";
    echo $timezone->getID() . "\n";
    echo $timezone->getRawOffset() . "\n";
} else {
    echo "無效的時區識別符號\n";
}

輸出:

China Standard Time
Asia/Shanghai
28800

上述示例中,我們使用 "Asia/Shanghai" 作為引數呼叫了 IntlTimeZone::createTimeZone() 函式來建立一個表示上海時區的 IntlTimeZone 物件。然後,我們使用該物件的一些方法來獲取時區的顯示名稱、時區識別符號和原始偏移量。如果時區識別符號無效,函式將返回 false,並輸出相應的錯誤訊息。

補充糾錯
熱門PHP函式
分享連結