PHP Conference Japan 2024

DateTimeZone 類別

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

簡介

時區的表示法。

類別概要

class DateTimeZone {
/* 常數 */
public const int AFRICA;
public const int AMERICA;
public const int ANTARCTICA;
公開 常數 整數 ARCTIC;
公開 常數 整數 ASIA;
公開 常數 整數 ATLANTIC;
公開 常數 整數 AUSTRALIA;
公開 常數 整數 EUROPE;
公開 常數 整數 INDIAN;
公開 常數 整數 PACIFIC;
公開 常數 整數 UTC;
公開 常數 整數 ALL;
公開 常數 整數 ALL_WITH_BC;
公開 常數 整數 PER_COUNTRY;
/* 方法 */
公開 __construct(字串 $timezone)
公開 getName(): 字串
公開 getTransitions(整數 $timestampBegin = PHP_INT_MIN, 整數 $timestampEnd = PHP_INT_MAX): 陣列|false
公開 靜態 listAbbreviations(): 陣列
公開 靜態 listIdentifiers(int $timezoneGroup = DateTimeZone::ALL, ?string $countryCode = null): array
}

預定義常數

DateTimeZone::AFRICA int

非洲時區。

DateTimeZone::AMERICA int

美洲時區。

DateTimeZone::ANTARCTICA int

南極洲時區。

DateTimeZone::ARCTIC int

北極時區。

DateTimeZone::ASIA int

亞洲時區。

DateTimeZone::ATLANTIC int

大西洋時區。

DateTimeZone::AUSTRALIA int

澳洲時區。

DateTimeZone::EUROPE int

歐洲時區。

DateTimeZone::INDIAN int

印度洋時區。

DateTimeZone::PACIFIC int

太平洋時區。

DateTimeZone::UTC int

UTC 時區。

DateTimeZone::ALL int

所有時區。

DateTimeZone::ALL_WITH_BC int

所有時區,包含向下相容的時區。

DateTimeZone::PER_COUNTRY int

依國家劃分的時區。

更新日誌

版本 說明
8.4.0 類別常數現在已鍵入。

目錄

新增筆記

使用者貢獻的筆記 1 則筆記

Jarmo Troska
1 年前
使用 DateTime 和 DateTimeZone 類別在不同時區之間轉換的範例。

請注意,PHP 也會負責計算相關的日光節約時間!

<?php

$utc_timezone
= new DateTimeZone("UTC");

$tallinn_timezone = new DateTimeZone("Europe/Tallinn");

// 建立一個新的 UTC 格式的 DateTime 物件

$datetime = new DateTime("2023-01-01 11:00:00", $utc_timezone);

// 將 DateTime 物件轉換為塔林時區

$datetime->setTimezone($tallinn_timezone);

// 以 YYYY-MM-DD HH:MM:SS 格式顯示結果

echo $datetime->format('Y-m-d H:i:s');

// 回傳:2023-01-01 13:00:00

?>
To Top