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条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题