The IntlTimeZone class

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

简介

类摘要

IntlTimeZone {
/* Constants */
const integer DISPLAY_SHORT = 1 ;
const integer DISPLAY_LONG = 2 ;
/* 方法 */
public static countEquivalentIDs ( string $zoneId ) : int
public static createDefault ( void ) : IntlTimeZone
public static createEnumeration ([ mixed $countryOrRawOffset ] ) : IntlIterator
public static createTimeZone ( string $zoneId ) : IntlTimeZone
public static createTimeZoneIDEnumeration ( int $zoneType [, string $region [, int $rawOffset ]] ) : IntlIterator
public static fromDateTimeZone ( DateTimeZone $zoneId ) : IntlTimeZone
public static getCanonicalID ( string $zoneId [, bool &$isSystemID ] ) : string
public getDisplayName ([ bool $isDaylight [, int $style [, string $locale ]]] ) : string
public getDSTSavings ( void ) : int
public static getEquivalentID ( string $zoneId , int $index ) : string
public getErrorCode ( void ) : int
public getErrorMessage ( void ) : string
public static getGMT ( void ) : IntlTimeZone
public getID ( void ) : string
public static getIDForWindowsID ( string $timezone [, string $region ] ) : string
public getOffset ( float $date , bool $local , int &$rawOffset , int &$dstOffset ) : int
public getRawOffset ( void ) : int
public static getRegion ( string $zoneId ) : string
public static getTZDataVersion ( void ) : string
public static getUnknown ( void ) : IntlTimeZone
public static getWindowsID ( string $timezone ) : string
public hasSameRules ( IntlTimeZone $otherTimeZone ) : bool
public toDateTimeZone ( void ) : DateTimeZone
public useDaylightTime ( void ) : bool
}

预定义常量

IntlTimeZone::DISPLAY_SHORT

IntlTimeZone::DISPLAY_LONG

Table of Contents

User Contributed Notes

Anonymous 13-Jul-2018 08:40
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;
?>