我有一个JSON,其中列出了国家/地区以及他们的省/省。这是一个简短的版本(没有显示所有示例)。我称之为 countries.json :
{
"USA": {
"NH": "New Hampshire",
"NJ": "New Jersey",
"NM": "New Mexico",
"NY": "New York",
"NC": "North Carolina",
"ND": "North Dakota",
"OH": "Ohio",
"OK": "Oklahoma",
"OR": "Oregon",
"PA": "Pennsylvania",
"RI": "Rhode Island",
"SC": "South Carolina",
"SD": "South Dakota",
"TN": "Tennessee",
"TX": "Texas"
},
"Canada": {
"ON": "Ontario",
"QC": "Quebec",
"NS": "Nova Scotia"
}
}
我的目标是构建以下表单:
我遇到的问题是某些状态具有不同的时区。例如,德克萨斯州。德克萨斯州有两个UTC补偿:
来源:https://en.wikipedia.org/wiki/List_of_time_offsets_by_U.S._state
使用PHP和/或Javascript / jQuery我该如何实现?
到目前为止,我有以下内容:
HTML :
<div>
<label>Country:</label>
<select id="country"></select>
</div>
<div id="states_container"></div>
<div id="timezones_container"></div>
JS:
$(document).ready(function () {
$.get("countries.json", function (json) {
$.each(json, function (k, v) {
$("#country").append('<option value="' + k + '">' + k + '</option>');
});
$("#country").change(function () {
var country_code = $(this).val();
var states = json[country_code];
var sel = '<label>States:</label><select>';
$.each(states, function (k, v) {
sel += '<option value="' + k + '">' + v + '</option>';
});
sel += '</select>';
$("#states_container").html(sel);
});
});
});
一个想法是使用以下内容在AJAX请求中使用 PHP :
<?php
$timezones = DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, "US");
var_dump($timezones);
/* Result */
/*
array (size=29)
0 => string 'America/Adak' (length=12)
1 => string 'America/Anchorage' (length=17)
2 => string 'America/Boise' (length=13)
3 => string 'America/Chicago' (length=15)
4 => string 'America/Denver' (length=14)
5 => string 'America/Detroit' (length=15)
6 => string 'America/Indiana/Indianapolis' (length=28)
7 => string 'America/Indiana/Knox' (length=20)
8 => string 'America/Indiana/Marengo' (length=23)
9 => string 'America/Indiana/Petersburg' (length=26)
10 => string 'America/Indiana/Tell_City' (length=25)
11 => string 'America/Indiana/Vevay' (length=21)
12 => string 'America/Indiana/Vincennes' (length=25)
13 => string 'America/Indiana/Winamac' (length=23)
14 => string 'America/Juneau' (length=14)
15 => string 'America/Kentucky/Louisville' (length=27)
16 => string 'America/Kentucky/Monticello' (length=27)
17 => string 'America/Los_Angeles' (length=19)
18 => string 'America/Menominee' (length=17)
19 => string 'America/Metlakatla' (length=18)
20 => string 'America/New_York' (length=16)
21 => string 'America/Nome' (length=12)
22 => string 'America/North_Dakota/Beulah' (length=27)
23 => string 'America/North_Dakota/Center' (length=27)
24 => string 'America/North_Dakota/New_Salem' (length=30)
25 => string 'America/Phoenix' (length=15)
26 => string 'America/Sitka' (length=13)
27 => string 'America/Yakutat' (length=15)
28 => string 'Pacific/Honolulu' (length=16)
*/
?>
但是这给了我每个国家/地区的时区(在这种情况下是美国),我需要它们来自州和会计UTC。
你们对如何实现这个目标有任何想法吗?任何帮助将不胜感激。
注意:我知道有些API可以提供帮助,但我正在寻找可以在我自己的服务器上托管的本地内容。