2013-06-11 21 views
1

我一直在做一些研究,为用户提供一个可用的时区选择框,但到目前为止我对结果并不满意。 我使用PHP的内置DateTime和DateTimeZone来获取时区列表(Olson数据库)。但是,如果你曾经使用过这个,你会发现它是一个很长的列表(约300个项目),因此在选择框中不可用。我使用optgroups对它进行了格式化,以便按区域对它进行分组,并使用相同的UTC偏移量连接城市。名单还很长,我对此并不满意。我实际上只想得到世界各个地区的主要城市,而不是相对未知的城市,如果这是有道理的话。PHP时区选择

我想保持标识符的样子,因为我可以使用DateTime和DateTimeZone类来执行转换和东西。任何想法如何使这个列表更有用/更短?

我现在所拥有的代码如下(有点凌乱,但现在它的工作正在进行中):

public function getListOfTimezones() 
    { 
     $options = array(); 
     $utc = new DateTimeZone('UTC'); 

     //$regions = array('Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific'); 
     $regions = array('America', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Pacific'); 
     $tzs = DateTimeZone::listIdentifiers(); 
     $optgroup = null; 
     $timezoneGroup = array(); 
     sort($tzs); 

     foreach ($tzs as $tz) 
     { 
      $timezonepieces = explode('/', $tz, 2); 

      if (count($timezonepieces) != 2 || !in_array($timezonepieces[0], $regions)) continue; 

      $region = $timezonepieces[0]; 
      $location = $timezonepieces[1];    

      if (count($timezoneGroup) > 0) 
      { 
       $lastTimeZone = $timezoneGroup[count($timezoneGroup) - 1]; 

       $utcTime = new DateTime(null, $utc); 
       $timezoneInfo = new DateTimeZone($tz); 
       $lastTimeZoneInfo = new DateTimeZone($lastTimeZone['timezoneid']); 

       if ($timezoneInfo->getOffset($utcTime) == $lastTimeZoneInfo->getOffset($utcTime)) 
       { 
        $addTz = array('timezoneid' => $tz, 'timezonelabel' => str_replace('_', ' ', $location)); 
        $timezoneGroup[] = $addTz; 
       } 
       else 
       { 
        $labels = array(); 

        if (count($timezoneGroup) > 5) 
        { 
         $timezoneGroup = array_slice($timezoneGroup, 0, 5); 
        } 

        $country = ''; 
        foreach ($timezoneGroup as $curTimezone) 
        { 
         $pieces = explode('/', $curTimezone['timezonelabel'], 2); 

         if (count($pieces) == 2) 
         { 
          if ($pieces[0] == $country) 
          { 
           continue; 
          } 
          else 
          { 
           $country = $pieces[0]; 
          } 
         } 

         $labels[] = $curTimezone['timezonelabel']; 
        } 
        $optgroup['options'][htmlentities($timezoneGroup[0]['timezoneid'])] = implode(', ', $labels); 

        // New timezone group 
        $timezoneGroup = array(); 
        $addTz = array('timezoneid' => $tz, 'timezonelabel' => str_replace('_', ' ', $location)); 
        $timezoneGroup[] = $addTz; 
       } 
      } 
      else 
      { 
       $addTz = array('timezoneid' => $tz, 'timezonelabel' => str_replace('_', ' ', $location)); 
       $timezoneGroup[] = $addTz; 
      } 

      if (!$optgroup || (is_array($optgroup) && array_key_exists('label', $optgroup) && $optgroup['label'] != htmlentities($region))) 
      { 
       if ($optgroup) 
       { 
        // End of previous optgroup, start of new one 
        $options[] = $optgroup; 
       } 

       $optgroup = array('label' => htmlentities($region), 'options' => array()); 
      } 
     } 

     if (is_array($optgroup)) 
     { 
      $options[] = $optgroup; 
     } 

     return $options; 
    } 
+2

使用JS来实现查找,当你键入?例如,['Select2'](http://ivaynberg.github.io/select2/)。 – Jon

+0

这是一个相当不错的组件。我会研究它,看看我能否把它扔进去。谢谢! – Ruben

+0

好吧,我实现了Select2选择框,这对于普通用户来说非常简单。非常感谢乔恩。既然你用评论回答我不能给你分。如果你把它写在答案中,我会接受它作为答案。 – Ruben

回答

2

是有限制的,当有需要,你可以用纯HTML做什么专门的数据输入就像这里一样。

我推荐使用一些增强时区选择的JavaScript组件;例如一个非常好的jQuery插件,我也使用的是Select2。它可以在您输入时自动查找并转换一个下拉菜单以及许多附加功能。