PHP 8.3.4 Released!

La clase IntlTimeZone

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

Introducción

Sinopsis de la Clase

class IntlTimeZone {
/* Constantes */
/* Métodos */
public static countEquivalentIDs(string $zoneId): integer
public static createDefault(): IntlTimeZone
public static createEnumeration(mixed $countryOrRawOffset = ?): IntlIterator
public static createTimeZone(string $zoneId): IntlTimeZone
public static createTimeZoneIDEnumeration(int $type, ?string $region = null, ?int $rawOffset = null): IntlIterator|false
intltz_create_time_zone_id_enumeration(int $type, ?string $region = null, ?int $rawOffset = null): IntlIterator|false
public static getCanonicalID(string $zoneId, bool &$isSystemID = ?): string
public getDisplayName(bool $isDaylight = ?, integer $style = ?, string $locale = ?): string
public static getEquivalentID(string $zoneId, integer $index): string
public static getGMT(): IntlTimeZone
public getID(): string
public static getIDForWindowsID(string $timezoneId, ?string $region = null): string|false
intltz_get_id_for_windows_id(string $timezoneId, ?string $region = null): string|false
public getOffset(
    float $date,
    bool $local,
    integer &$rawOffset,
    integer &$dstOffset
): integer
public static getRegion(string $timezoneId): string|false
intltz_get_region(string $timezoneId): string|false
public static getTZDataVersion(): string
public static getUnknown(): IntlTimeZone
intltz_get_unknown(): IntlTimeZone
public static getWindowsID(string $timezoneId): string|false
intltz_get_windows_id(string $timezoneId): string|false
public hasSameRules(IntlTimeZone $otherTimeZone): bool
}

Tabla de contenidos

add a note

User Contributed Notes 1 note

up
2
Anonymous
5 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