PHP 8.4.2 Released!

The IntlTimeZone class

(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_SHORT_GMT;
public const int DISPLAY_LONG_GMT;
public const int TYPE_ANY;
public const int TYPE_CANONICAL;
/* 方法 */
private __construct()
public static countEquivalentIDs(string $timezoneId): int|false
public static createDefault(): IntlTimeZone
public static createEnumeration(IntlTimeZone|string|int|float|null $countryOrRawOffset = null): IntlIterator|false
public static createTimeZone(string $timezoneId): ?IntlTimeZone
public static createTimeZoneIDEnumeration(int $type, ?string $region = null, ?int $rawOffset = null): IntlIterator|false
public static fromDateTimeZone(DateTimeZone $timezone): ?IntlTimeZone
public static getCanonicalID(string $timezoneId, bool &$isSystemId = null): string|false
public getDisplayName(bool $dst = false, int $style = IntlTimeZone::DISPLAY_LONG, ?string $locale = null): string|false
public getDSTSavings(): int
public static getEquivalentID(string $timezoneId, int $offset): string|false
public static getGMT(): IntlTimeZone
public getID(): string|false
public static getIDForWindowsID(string $timezoneId, ?string $region = null): string|false
public getOffset(
    float $timestamp,
    bool $local,
    int &$rawOffset,
    int &$dstOffset
): bool
public getRawOffset(): int
public static getRegion(string $timezoneId): string|false
public static getTZDataVersion(): string|false
public static getUnknown(): IntlTimeZone
public static getWindowsID(string $timezoneId): string|false
}

更新日志

版本 说明
8.4.0 The class constants are now typed.

目录

添加备注

用户贡献的备注 1 note

up
2
Anonymous
6 years ago
Currently there is no documentation available for IntlTimeZone::parse(), so it took some while to figure out a working example:

<?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