2024 日本 PHP 研討會

IntlTimeZone 類別

(PHP 5 >= 5.5.0, PHP 7, PHP 8, PECL >= 3.0.0a1)

簡介

類別概要

class IntlTimeZone {
/* 常數 */
public const int DISPLAY_SHORT;
public const int DISPLAY_LONG;
public const int DISPLAY_LONG_GENERIC;

(公開常數)顯示長的一般時區名稱。

public const int DISPLAY_SHORT_GMT;

(公開常數)顯示短的 GMT 時區名稱。

public const int DISPLAY_LONG_GMT;

(公開常數)顯示長的 GMT 時區名稱。

public const int DISPLAY_SHORT_COMMONLY_USED;

(公開常數)顯示短的常用時區名稱。

public const int DISPLAY_GENERIC_LOCATION;

(公開常數)顯示一般位置時區名稱。

public const int TYPE_ANY;

(公開常數)任何類型的時區。

public const int TYPE_CANONICAL;

(公開常數)標準時區。

public const int TYPE_CANONICAL_LOCATION;

(公開常數)標準位置時區。

/* 方法 */
private __construct()

(私有方法)建構子。

public static countEquivalentIDs(string $timezoneId): int|false

(公開靜態方法)計算等效的時區 ID 數量。

public static createDefault(): IntlTimeZone

(公開靜態方法)建立預設的 IntlTimeZone 物件。

public static createEnumeration(IntlTimeZone|string|int|float|null $countryOrRawOffset = null): IntlIterator|false

(公開靜態方法)建立時區列舉器。

public static createTimeZone(string $timezoneId): ?IntlTimeZone

(公開靜態方法)使用指定的時區 ID 建立 IntlTimeZone 物件。

公開 靜態 createTimeZoneIDEnumeration(整數 $type, ?字串 $region = null, ?整數 $rawOffset = null): IntlIterator|false
公開 靜態 fromDateTimeZone(DateTimeZone $timezone): ?IntlTimeZone
公開 靜態 getCanonicalID(字串 $timezoneId, 布林值 &$isSystemId = null): 字串|false
公開 getDisplayName(布林值 $dst = false, 整數 $style = IntlTimeZone::DISPLAY_LONG, ?字串 $locale = null): 字串|false
公開 靜態 getEquivalentID(字串 $timezoneId, 整數 $offset): 字串|false
public static getGMT(): IntlTimeZone
public getID(): 字串|false
public static getIDForWindowsID(字串 $timezoneId, ?字串 $region = null): 字串|false
public getOffset(
    浮點數 $timestamp,
    布林值 $local,
    整數 &$rawOffset,
    整數 &$dstOffset
): 布林值
public static getRegion(字串 $timezoneId): 字串|false
public static getTZDataVersion(): 字串|false
public static getUnknown(): IntlTimeZone
public static getWindowsID(字串 $timezoneId): 字串|false
}

更新日誌

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

目錄

新增筆記

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

2
匿名
6 年前
目前沒有 IntlTimeZone::parse() 的文件,因此需要一些時間才能找到一個有效的範例

<?php
// Example for 32-bit PHP 7.0.30 with Intl version 1.1, ICU version 56.1
// To get the offset of a date including daylight saving time, getRawOffset() is
// not sufficient, getOffset() is needed. And a date is needed to take history
// in to account.

\Locale::setDefault('nl-NL');
$date = '25-03-2018 12:34:56.123456 CEST'; // 3rd of Februari
$timezone = \IntlTimeZone::createDefault(); // = CEST = GMT +02:00
print 'Time zone: ' . $timezone->getDisplayName() . PHP_EOL;
print
'DST: ' . ($timezone->useDaylightTime() ? 'DOES' : 'DOES NOT')
.
' happen to this timezone' . PHP_EOL;
print
'Possible DST difference (usec): ' . $timezone->getDSTSavings() . PHP_EOL;

$formatter = new \IntlDateFormatter(
\Locale::getDefault(),
\IntlDateFormatter::MEDIUM,
\IntlDateFormatter::MEDIUM,
$timezone
);
$timestamp = $formatter->parse($date);
if (
FALSE === $timestamp) {
throw new
\Exception('Can not parse date');
}
elseif (
is_float($timestamp)) {
throw new
\Exception('Y2K38 bug @ 32 bit, please use 64 bit');
}
print
'Date parsed by IntlFormatter::parse(): ' . date('c', $timestamp)
.
PHP_EOL;

$cal = \IntlCalendar::createInstance();
$cal->setTimeZone($timezone);
$cal->setLenient(FALSE);
// set UNIX timestamp offset (1970-01-01 00:00:00.000000) and add the timestamp
$cal->set(1970, 0, 1, 0, 0, 0);
$cal->set(\IntlCalendar::FIELD_MILLISECOND, 0); // note: msec, not usec
$cal->add(\IntlCalendar::FIELD_SECOND, $timestamp); // note: no milliseconds
$cal->add(\IntlCalendar::FIELD_MILLISECOND, 124); // hardcode for simplicity

header('Content-type: text/plain');
print
'Date constructed with IntlCalendar: ' . $formatter->format($cal)
.
PHP_EOL;
print
'Raw offset by getRawOffset(): ' . $timezone->getRawOffset() . PHP_EOL;

/**
* Function IntlTimeZone::getOffset()
* @link http://icu-project.org/apiref/icu4c/classicu_1_1TimeZone.html#afcbc1c48bf0b453b0123c4cb75d20e96
* @param float $date
* moment in time for which to return offsets, in units of milliseconds from
* January 1, 1970 0:00 GMT, either GMT time or local wall time, depending on
* `local'.
* @param bool $local
* if true, `date' is local wall time; otherwise it is in GMT time.
* @param int &$rawOffset
* output parameter to receive the raw offset, that is, the offset not
* including DST adjustments
* @param int &$dstOffset
* output parameter to receive the DST offset, that is, the offset to be added
* to `rawOffset' to obtain the total offset between local and GMT time. If
* DST is not in effect, this value is zero; otherwise it is a positive value,
* typically one hour.
*/

$rawOffset = NULL;
$dstOffset = NULL;
$timestamp *= 1000.0; // convert unix timestamp from secs to msecs
$timezone->getOffset($timestamp, $local = FALSE, $rawOffset, $dstOffset);
print
'Output of getOffset():' . PHP_EOL . json_encode([
'rawOffset' => $rawOffset,
'dstOffset' => $dstOffset
], JSON_PRETTY_PRINT) . PHP_EOL;
?>
To Top