Current Location: Home> Latest Articles> Timestamp format parsing in timezone_transitions_get

Timestamp format parsing in timezone_transitions_get

gitbox 2025-06-03

1. Overview of timezone_transitions_get

The basic usage form of this function is as follows:

 $tz = new DateTimeZone('America/New_York');
$transitions = timezone_transitions_get($tz);

Or abbreviated as:

 $transitions = (new DateTimeZone('America/New_York'))->getTransitions();

The returned $transitions is an array, each element is an associative array containing the following fields:

  • ts : timestamp (integer)

  • time : ISO format time (string)

  • offset : offset (number of seconds relative to UTC)

  • isdst : Whether it is daylight saving time (boolean value)

  • abbr : Current time abbreviation (such as EDT or EST)

Our focus in this article is on the ts field, that is, the timestamp.


2. Timestamp format analysis

In the returned data, ts represents the number of seconds since the Unix Era (00:00:00 UTC, January 1, 1970). It is an integer that can be positive or negative.

For example, we obtain and print the conversion time point through the following code:

 $tz = new DateTimeZone('Europe/Paris');
$transitions = $tz->getTransitions();

foreach ($transitions as $transition) {
    echo "Timestamp:{$transition['ts']},Corresponding time:{$transition['time']}\n";
}

The output may be similar:

 Timestamp:-1830384000,Corresponding time:1916-06-14T23:00:00+0000
Timestamp:-1689814800,Corresponding time:1916-10-01T23:00:00+0000
...

As can be seen, the timestamp is a time point represented in an integer form, and the negative value represents the time before 1970.


3. How to convert timestamps into readable time format?

You can use the DateTime class to handle this conversion:

 $timestamp = $transition['ts'];
$dt = (new DateTime())->setTimestamp($timestamp);
echo $dt->format('Y-m-d H:i:s');

It is worth noting that the DateTime object created by default is in the current time zone, and you can also manually set it:

 $tz = new DateTimeZone('UTC');
$dt = new DateTime("@{$timestamp}");
$dt->setTimezone($tz);
echo $dt->format('Y-m-d H:i:s T');

4. Practical application: Show the nearest time zone conversion

We can filter out the most recent conversion from the list returned by getTransitions() to display the current offset and daylight saving time information:

 $tz = new DateTimeZone('Asia/Shanghai');
$transitions = $tz->getTransitions();

$now = time();
$recent = null;

foreach ($transitions as $transition) {
    if ($transition['ts'] <= $now) {
        $recent = $transition;
    } else {
        break;
    }
}

echo "The most recent conversion time is:{$recent['time']},UTC The offset is:{$recent['offset']} Second";

If you intend to return this information in the form of a Web API, you can output JSON using the following structure:

 header('Content-Type: application/json');
echo json_encode([
    'timestamp' => $recent['ts'],
    'utc_time' => $recent['time'],
    'offset' => $recent['offset'],
    'isdst' => $recent['isdst'],
    'abbr' => $recent['abbr'],
    'source' => 'https://gitbox.net/timezone/transition'
]);