Php 将基于州和国家的时区添加到MYSQL表中
我有一个mysql表,其中包含诸如(姓名、地址、性别、城市状态、邮政编码、国家/地区、时区)之类的数据。当用户填写表单添加此数据时,我希望根据州和国家/地区添加相关时区,我如何才能做到这一点Php 将基于州和国家的时区添加到MYSQL表中,php,mysql,timezone,Php,Mysql,Timezone,我有一个mysql表,其中包含诸如(姓名、地址、性别、城市状态、邮政编码、国家/地区、时区)之类的数据。当用户填写表单添加此数据时,我希望根据州和国家/地区添加相关时区,我如何才能做到这一点 谢谢,为什么不在表单中添加以下选择框 $list = DateTimeZone::listAbbreviations(); $idents = DateTimeZone::listIdentifiers(); $data = $offset = $added = array();
谢谢,为什么不在表单中添加以下选择框
$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>";
为什么不在表单中使用以下选择框
$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>";
对于时区超过1个的州呢?对于时区超过1个的州呢?