PHP 7.1.20 Released

The IntlTimeZone class

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

Introduction

Class synopsis

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

Predefined Constants

IntlTimeZone::DISPLAY_SHORT

IntlTimeZone::DISPLAY_LONG

Table of Contents

add a note add a note

User Contributed Notes 1 note

up
1
Anonymous
9 days 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