dqunzip3183 2017-04-11 02:19
浏览 354
已采纳

PHP - 获取时区缩写,包括夏令时

PHP

$city = "Australia/NSW";
function get_timezone_abre()
{   
    global $city;
    $dateTime = new DateTime(); 
    $dateTime->setTimeZone(new DateTimeZone($city )); 
    return 'A'.$dateTime->format('T'); 
}

Result

AEST

Not sure either this will show daylight saving has started, can someone clarify this please?

Looked at this link, what-are-the-standard-timezone-abbreviations.

Can someone suggest a better way to do this (includes daylight saving)?

Add reading material

Australia DST timezone abbreviation incorrect when using date_default_timezone_set

  • Base on this, I assume my abbreviation EST is derived from using older php lib.
  • 写回答

2条回答 默认 最新

  • dpiuqwwyu187975836 2017-04-11 04:37
    关注

    Australia/NSW is not a valid timezone identifier. Valid timezone identifiers for Australia are:

    [299] => Australia/Adelaide
    [300] => Australia/Brisbane
    [301] => Australia/Broken_Hill
    [302] => Australia/Currie
    [303] => Australia/Darwin
    [304] => Australia/Eucla
    [305] => Australia/Hobart
    [306] => Australia/Lindeman
    [307] => Australia/Lord_Howe
    [308] => Australia/Melbourne
    [309] => Australia/Perth
    [310] => Australia/Sydney
    

    You can find this list by using DateTimeZone::listIdentifiers() (see reference).

    Australia/NSW is valid but this comment here says it was included for backwards compatibility and could be removed.

    This code should do what you need, it considers the daylight savings changes too:

    <?php
    $city = "Australia/NSW";
    $timezone = new DateTimeZone($city);
    $date_time = new DateTime('now', $timezone);
    
    echo $date_time->format('T') . PHP_EOL;
    echo $date_time->modify('+6 months')->format('T');
    

    This will output:

    AEST
    AEDT
    

    Demo here: https://eval.in/772564

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?