略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: DateTimeZone::getTransitions

2024-12-22

DateTimeZone::getTransitions

timezone_transitions_get

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

DateTimeZone::getTransitions -- timezone_transitions_getReturns all transitions for the timezone

说明

面向对象风格

public DateTimeZone::getTransitions(int $timestampBegin = PHP_INT_MIN, int $timestampEnd = PHP_INT_MAX): array|false

过程化风格

timezone_transitions_get(DateTimeZone $object, int $timestampBegin = PHP_INT_MIN, int $timestampEnd = PHP_INT_MAX): array|false

参数

object

仅过程化风格:由 timezone_open() 返回的 DateTimeZone 对象。

timestampBegin

Begin timestamp.

timestampEnd

End timestamp.

返回值

Returns a numerically indexed array of transition arrays on success, 或者在失败时返回 false. DateTimeZone objects wrapping type 1 (UTC offsets) and type 2 (abbreviations) do not contain any transistions, and calling this method on them will return false.

If timestampBegin is given, the first entry in the returned array will contain a transition element at the time of timestampBegin.

Transition Array Structure
Key Type Description
ts int Unix timestamp
time string DateTimeInterface::ISO8601 time string
offset int Offset to UTC in seconds
isdst bool Whether daylight saving time is active
abbr string Timezone abbreviation

范例

示例 #1 A timezone_transitions_get() example

<?php
$timezone 
= new DateTimeZone("Europe/London");
$transitions $timezone->getTransitions();
print_r(array_slice($transitions03));
?>

以上例程的输出类似于:

Array
(
    [0] => Array
        (
            [ts] => -9223372036854775808
            [time] => -292277022657-01-27T08:29:52+0000
            [offset] => 3600
            [isdst] => 1
            [abbr] => BST
        )

    [1] => Array
        (
            [ts] => -1691964000
            [time] => 1916-05-21T02:00:00+0000
            [offset] => 3600
            [isdst] => 1
            [abbr] => BST
        )

    [2] => Array
        (
            [ts] => -1680472800
            [time] => 1916-10-01T02:00:00+0000
            [offset] => 0
            [isdst] => 
            [abbr] => GMT
        )

)

示例 #2 A timezone_transitions_get() example with timestampBegin set

<?php
$timezone 
= new DateTimeZone("Europe/London");
$transitions $timezone->getTransitions(time());
print_r(array_slice($transitions03));
?>

以上例程的输出类似于:

Array
(
    [0] => Array
        (
            [ts] => 1654184161
            [time] => 2022-06-02T15:36:01+0000
            [offset] => 3600
            [isdst] => 1
            [abbr] => BST
        )

    [1] => Array
        (
            [ts] => 1667091600
            [time] => 2022-10-30T01:00:00+0000
            [offset] => 0
            [isdst] => 
            [abbr] => GMT
        )

    [2] => Array
        (
            [ts] => 1679792400
            [time] => 2023-03-26T01:00:00+0000
            [offset] => 3600
            [isdst] => 1
            [abbr] => BST
        )

)
add a noteadd a note

User Contributed Notes 4 notes

up
9
scotts
12 years ago
Getting a timezone's offset and other data at a specified point in time (now, for example) using the new PHP5.3 parameters:

<?php
$theTime
= time(); # specific date/time we're checking, in epoch seconds.

$tz = new DateTimeZone('America/Los_Angeles');
$transition = $tz->getTransitions($theTime,$theTime);

# only one array should be returned into $transition. Now get the data:
$offset = $transition[0]['offset'];
$abbr = $transition[0]['abbr'];
?>

Before PHP5.3, you'd have to loop through all of the Transitions up until the current point in time, which would be a very inefficient process.
up
3
rubo77
8 years ago
This will work  in PHP_VERSION < 5.3:

<?php
/** returns an array with two elements for spring and fall DST in a given year
*  works in PHP_VERSION < 5.3
*
* @param integer $year
* @param string $tz timezone
* @return array
**/
function getTransitionsForYear($year=null, $tz = null){
    if(!
$year) $year=date("Y");

    if (!
$tz) $tz = date_default_timezone_get();
   
$timeZone = new DateTimeZone($tz);

    if (
version_compare(PHP_VERSION, '5.3.0') >= 0) {
       
$transitions = $timeZone->getTransitions(mktime(0, 0, 0, 2, 1, $year),mktime(0, 0, 0, 11, 31, $year));
       
$index=1;
    } else {
       
// since 1980 it is regular, the 29th element is 1980-04-06
            // change this in your timezone
           
$first_regular_index=29;
           
$first_regular_year=1980;
       
$transitions = $timeZone->getTransitions();
       
$index=($year-$first_regular_year)*2+$first_regular_index;
    }
    return
array_slice($transitions, $index, 2);
}
up
0
tanguy dot pruvot at gmail dot com
8 years ago
Beware if you use getTransitions() without a minimum date, it can be slow...
up
-3
rubo77
8 years ago
This function will work in PHP < 5.3 :

/** returns an array with two elements for spring and fall DST in a given year
*
* @param integer $year
* @param string $tz timezone
* @return array
**/
function getTransitionsForYear($year=null, $tz = null){
    if(!$year) $year=date("Y");
   
    if (!$tz) $tz = date_default_timezone_get();
    $timeZone = new DateTimeZone($tz);
   
    $transitions = $timeZone->getTransitions(mktime(0, 0, 0, 1, 1, $year));
    return array_slice($transitions, 1, 2);
}

官方地址:https://www.php.net/manual/en/datetimezone.gettransitions.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3