doubutao6216 2017-02-22 12: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 12: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 根据历年月数据,用Stata预测未来六个月汇率
  • ¥15 DevEco studio开发工具 真机联调找不到手机设备
  • ¥15 请教前后端分离的问题
  • ¥100 冷钱包突然失效,急寻解决方案
  • ¥15 下载honeyd时报错 configure: error: you need to instal a more recent version of libdnet
  • ¥15 距离软磁铁一定距离的磁感应强度大小怎么求
  • ¥15 霍尔传感器hmc5883l的xyz轴输出和该点的磁感应强度大小的关系是什么
  • ¥15 vscode开发micropython,import模块出现异常
  • ¥20 Excel数据自动录入表单并提交
  • ¥30 silcavo仿真,30分钟,只需要代码
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部