DateTimeZone::__construct()
是 PHP DateTimeZone 類的建構函式。它用於建立一個新的 DateTimeZone 物件,並可以指定時區識別符號。
使用語法如下:
DateTimeZone::__construct(string $timezone)
引數 $timezone
是一個字串,表示時區識別符號。它可以是一個有效的時區名稱,也可以是一個 UTC 的偏移量,形如 "+HH:MM" 或 "-HH:MM"。
下面是一個示例:
$timezone = new DateTimeZone('Asia/Shanghai');
這裡,我們建立了一個名為 $timezone
的 DateTimeZone 物件,該物件代表了中國上海的時區。
另外,你還可以使用以下方法獲取可用的時區識別符號:
$timezone_identifiers = DateTimeZone::listIdentifiers();
這會返回一個包含所有可用時區識別符號的陣列。
值得注意的是,DateTimeZone 物件在 DateTime 和 DateTimeImmutable 類中作為引數來設定時區。