我有一个mysql表的数据(姓名,地址,性别,城市州,zip,国家,时区)当用户填写表格添加此数据时,我想根据州和国家添加相关时区,我该如何做到这一点?
谢谢,
为什么不在表单中使用下面的选择框呢?
$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']
是所选select选项的处理程序,然后您可以从单个字段中提取时区,国家和州。
$locale=explode("/",$_POST['locale']);
// $locale[0] = timezone
// $locale[1] = country
// $locale[2-n] = state(s)