【问题标题】:Adding time-zones based on state and country into MYSQL table将基于州和国家的时区添加到 MYSQL 表中
【发布时间】:2011-10-12 15:51:39
【问题描述】:

我有一个 mysql 表,其中包含(姓名、地址、性别、城市州、邮编、国家、时区)等数据 当用户填写表格以添加此数据时,我希望添加相关的时区根据州和国家/地区,我该怎么做?

谢谢,

【问题讨论】:

  • 对于超过 1 个时区的州呢?

标签: php mysql timezone


【解决方案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)

【讨论】:

    猜你喜欢
    • 2021-04-07
    • 1970-01-01
    • 2016-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多