将基于州和国家/地区的时区添加到 MYSQL 表中

发布于 2024-12-09 18:25:07 字数 106 浏览 0 评论 0原文

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

谢谢,

I have a mysql table with data like (name, address, sex, city state, zip, country, timezone) When a user fills in the form to add this data, i would like the relevant time-zone to be added based on state and country, how can i do this?

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅语花开 2024-12-16 18:25:07

为什么不在您的表单中添加以下选择框?

 $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)

Why not have the following select box in your form?

 $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>";

This would handle the fact there are states with multiple timezones. When you then get the result of this form submission, you can explode the value of the field on '/', where $_POST['locale'] is the handler for the chosen select option, then you can extract timezone, country and state from a single field.

$locale=explode("/",$_POST['locale']);
// $locale[0] = timezone
// $locale[1] = country
// $locale[2-n] = state(s)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文