2011-10-12 32 views
1

我有一個類似於(姓名,地址,性別,城市狀態,郵編,國家,時區)數據的mysql表格當用戶填寫表單以添加此數據,我希望根據國家和國家添加相關時區,我該怎麼做?將基於州和國家的時區添加到MYSQL表中

謝謝,

+2

對於超過1個時區的州,情況如何? – Josh

回答

1

爲什麼不在你的窗體中有以下選擇框?

$list = DateTimeZone::listAbbreviations(); 

    $idents = DateTimeZone::listIdentifiers(); 

    $data = $offset = $added = array(); 
    foreach ($list as $abbr => $info) { 
     foreach ($info as $zone) { 
      if (! empty($zone['timezone_id']) 
       AND 
       ! in_array($zone['timezone_id'], $added) 
       AND 
        in_array($zone['timezone_id'], $idents)) { 
       $z = new DateTimeZone($zone['timezone_id']); 
       $c = new DateTime(null, $z); 
       $zone['time'] = $c->format('H:i a'); 
       $data[] = $zone; 
       $offset[] = $z->getOffset($c); 
       $added[] = $zone['timezone_id']; 
      } 
     } 
    } 

    array_multisort($added, SORT_ASC, $data); 
    $options = array(); 
    foreach ($data as $key => $row) { 
     $options[$row['timezone_id']] = $row['time'] . ' - ' 
             . formatOffset($row['offset']) 
             . ' ' . $row['timezone_id']; 
     $values[$row['timezone_id']] = $row['time'] . '/' 
             . formatOffset($row['offset']) 
             . '/' . $row['timezone_id']; 
    } 


function formatOffset($offset) { 
     $hours = $offset/3600; 
     $remainder = $offset % 3600; 
     $sign = $hours > 0 ? '+' : '-'; 
     $hour = (int) abs($hours); 
     $minutes = (int) abs($remainder/60); 

     if ($hour == 0 AND $minutes == 0) { 
      $sign = ' '; 
     } 
     return 'GMT' . $sign . str_pad($hour, 2, '0', STR_PAD_LEFT) 
       .':'. str_pad($minutes,2, '0'); 

} 

echo "<select name='locale'>"; 
foreach($options as $key=>$value){ 
    echo "<option value='".$values[$key]."'>".$value."</option>"; 
} 
echo "</select>"; 

這將處理有多個時區的狀態。當你得到這個表單提交的結果時,你可以在'/'上分解該字段的值,其中$_POST['locale']是所選擇的選項的處理程序,那麼你可以從一個字段中提取時區,國家和州。

$locale=explode("/",$_POST['locale']); 
// $locale[0] = timezone 
// $locale[1] = country 
// $locale[2-n] = state(s)