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

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

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services