doubutao6216 2017-02-22 20:34
浏览 52
已采纳

TIMEZONE选择框仅更改为美国

I have a bit of code that populates a selectbox with timezones however it includes Asia, Africa, etc. I would like to only have it list US timezones. Is that possible? The code I have now is...

    /*
    Get all timezone listing
    */
    function event_tz_list() {
      $zones_array = array();
      $timestamp = time();
      foreach(timezone_identifiers_list() as $key => $zone) {
        date_default_timezone_set($zone);
        $zones_array[$key]['zone'] = $zone;
        $zones_array[$key]['offset'] = date('P', $timestamp);
        $zones_array[$key]['diff_from_GMT'] = 'UTC/GMT ' . date('P', 
    $timestamp);
      }
      return $zones_array;

    }

Any information would be greatly appreciated!

  • 写回答

1条回答 默认 最新

  • dongyumiao5210 2017-02-22 20:52
    关注

    timezone_identifiers_list() takes two arguments. A constant for what timezones to get and a two letter country code if the first argument is DateTimeZone::PER_COUNTRY. So this will pretty much get it:

    timezone_identifiers_list(DateTimeZone::PER_COUNTRY, 'US')
    

    If you want to get the 7 abbreviations instead of the 29 America/City values:

    function event_tz_list() {
        foreach(timezone_identifiers_list(DateTimeZone::PER_COUNTRY, 'US') as $key => $zone) {
            $timestamp = time();
            date_default_timezone_set($zone);
            $abbrev = date('T'); // use as the key and you won't get duplicates
            $zones_array[$abbrev]['zone'] = $abbrev;
            $zones_array[$abbrev]['offset'] = date('P', $timestamp);
            $zones_array[$abbrev]['diff_from_GMT'] = 'UTC/GMT ' . date('P', $timestamp);
        }
        return $zones_array;
    }
    
    print_r(event_tz_list());
    

    Yields:

    Array
    (
        [HAST] => Array
            (
                [zone] => HAST
                [offset] => -10:00
                [diff_from_GMT] => UTC/GMT -10:00
            )
    
        [AKST] => Array
            (
                [zone] => AKST
                [offset] => -09:00
                [diff_from_GMT] => UTC/GMT -09:00
            )
    
        [MST] => Array
            (
                [zone] => MST
                [offset] => -07:00
                [diff_from_GMT] => UTC/GMT -07:00
            )
    
        [CST] => Array
            (
                [zone] => CST
                [offset] => -06:00
                [diff_from_GMT] => UTC/GMT -06:00
            )
    
        [EST] => Array
            (
                [zone] => EST
                [offset] => -05:00
                [diff_from_GMT] => UTC/GMT -05:00
            )
    
        [PST] => Array
            (
                [zone] => PST
                [offset] => -08:00
                [diff_from_GMT] => UTC/GMT -08:00
            )
    
        [HST] => Array
            (
                [zone] => HST
                [offset] => -10:00
                [diff_from_GMT] => UTC/GMT -10:00
            )
    
    )
    

    Which shows 7 timezones (I learned something):

    There is no time difference between Hawaii-Aleutian Standard Time (HAST) and Hawaii Standard Time (HST).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程