使用php和html分隔国家和城市

时间:2016-05-05 03:06:52

标签: php html timezone

这是代码的工作方式使用下拉菜单/选择国家/地区 - 城市 - (时区)我的问题是如何将城市和时区与国家分开。

请帮助我。

html code
<label for="timeZone"><br>City<br />
		<select id="timeZone" name="timeZone">
			<?php echo displayTimeZoneSelect("Europe/Amsterdam");	
			?>
		</select>
	</label>
	

 PHP CODE
    /**
     * returns a HTML formated TimeZone select
     *
     * @param $selectedTimeZone string The timezone marked as "selected"
     * @return string
     */

    function displayTimeZoneSelect($selectedTimeZone = 'America/New_York'){
    	$countryCodes = getCountryCodes();
    	$return = null;
    	foreach ($countryCodes as $country => $countryCode) {
    		$timezone_identifiers = DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY,$countryCode);
    		foreach( $timezone_identifiers as $value ){
    			/* getTimeZoneOffset returns minutes and we need to display hours */
    			$offset = getTimeZoneOffset($value)/60;
    			/* for the GMT+1 GMT-1 display */
    			$offset = ( substr($offset,0,1) == "-" ? " (GMT" : " (GMT+" ) . $offset . ")";
    			/* America/New_York -> America/New York */
    			$displayValue = (str_replace('_',' ',$value));
    			/* Find the city */
    			$ex = explode("/",$displayValue);
    			$city = ( ($ex[2]) ? $ex[2] : $ex[1] );
    			/* For the special names */
    			$displayValue = htmlentities($country." - ".$city.$offset); 
    			/* handle the $selectedTimeZone in the select form */
    			$selected = ( ($value == $selectedTimeZone) ? ' selected="selected"' : null );
    			$return .= '<option value="' . $value . '"' . $selected . '>'
    				. $displayValue
    				. '</option>' . PHP_EOL;			
    		}
    	}
    	return $return;
    }


    function getCountryCodes(){
    $return = array(
    "Afghanistan (‫افغانستان‬‎)"=>"AF",
    "Åland Islands (Åland)"=>"AX",
    "Albania (Shqipëri"=>"AL",
    "Algeria (‫الجزائر‬‎)"=>"DZ",
    "American Samoa"=>"AS",
    "Andorra"=>"AD",
    "Angola"=>"AO",
    "Anguilla"=>"AI",
    "Antarctica"=>"AQ",
    "Antigua and Barbuda"=>"AG",
    "Argentina"=>"AR",
    "Armenia (Հայաստան)"=>"AM",
    "Aruba"=>"AW",
    "Ascension Island"=>"AC",
    "Australia"=>"AU",
    "Austria (Österreich)"=>"AT",
    "Azerbaijan (Azərbaycan)"=>"AZ",
    "Bahamas"=>"BS",
    "Bahrain (‫البحرين‬‎)"=>"BH",
    "Bangladesh (বাংলাদেশ)"=>"BD",
    "Barbados"=>"BB",
    "Belarus (Беларусь)"=>"BY",
    "Belgium (België)"=>"BE",
    "Belize"=>"BZ",
    "Benin (Bénin)"=>"BJ",
    "Bermuda"=>"BM",
    "Bhutan (འབྲུག)"=>"BT",
    "Bolivia"=>"BO",
    "BONAIRE, SINT EUSTATIUS AND SABA"=>"BQ",
    "Bosnia and Herzegovina"=>"BA",
    "Botswana"=>"BW",
    "Bouvet Island"=>"BV",
    "Brazil (Brasil)"=>"BR",
    "British Indian Ocean Territory"=>"IO",
    "British Virgin Islands"=>"VG",
    "Brunei"=>"BN",
    "Bulgaria (България)"=>"BG",
    "Burkina Faso"=>"BF",
    "Burundi (Uburundi)"=>"BI",
    "Cambodia (កម្ពុជា)"=>"KH",
    "Cameroon (Cameroun)"=>"CM",
    "Canada"=>"CA",
    "Canary Islands (islas Canarias)"=>"IC",
    "Cape Verde (Kabu Verdi)"=>"CV",
    "Caribbean Netherlands"=>"BQ",
    "Cayman Islands"=>"KY",
    "Central African Republic"=>"CF",
    "Ceuta and Melilla"=>"EA",
    "Chad "=>"TD",
    "Chile"=>"CL",
    "China (中国)"=>"CN",
    "Christmas Island"=>"CX",
    "Clipperton Island"=>"CP",
    "Cocos (Keeling) Islands"=>"CC",
    "Colombia"=>"CO",
    "Comoros (‫جزر القمر‬‎)"=>"KM",
    "Congo (Republic)"=>"CG",
    "Congo (DRC)"=>"CD",
    "Cook Islands"=>"CK",
    "Costa Rica"=>"CR",
    "Côte d’Ivoire"=>"CI",
    "Croatia"=>"HR",
    "Cuba"=>"CU",
    "Curaçao"=>"CW",
    "Cyprus"=>"CY",
    "Czech Republic"=>"CZ",
    "Denmark"=>"DK",
    "Diego Garcia"=>"DG",
    "Djibouti"=>"DJ",
    "Dominica"=>"DM",
    "Dominican Republic"=>"DO",
    "Ecuador"=>"EC",
    "Egypt"=>"EG",
    "El Salvador"=>"SV",
    "Equatorial Guinea"=>"GQ",
    "Eritrea"=>"ER",
    "Estonia"=>"EE",
    "Ethiopia"=>"ET",
    "Falkland Islands (Islas Malvinas)"=>"FK",
    "Faroe Islands"=>"FO",
    "Fiji"=>"FJ",
    "Finland"=>"FI",
    "France"=>"FR",
    "French Guiana"=>"GF",
    "French Polynesia"=>"PF",
    "French Southern Territories"=>"TF",
    "Gabon"=>"GA",
    "Gambia"=>"GM",
    "Georgia"=>"GE",
    "Germany"=>"DE",
    "Ghana"=>"GH",
    "Gibraltar"=>"GI",
    "Greece"=>"GR",
    "Greenland"=>"GL",
    "Grenada"=>"GD",
    "Guadeloupe"=>"GP",
    "Guam"=>"GU",
    "Guatemala"=>"GT",
    "Guernsey"=>"GG",
    "Guinea"=>"GN",
    "Guinea-Bissau"=>"GW",
    "Guyana"=>"GY",
    "Haiti"=>"HT",
    "Heard & McDonald Islands"=>"HM",
    "Honduras"=>"HN",
    "Hong Kong (香港)"=>"HK",
    "Hungary"=>"HU",
    "Iceland"=>"IS",
    "India"=>"IN",
    "Indonesia"=>"ID",
    "Iran (‫ایران‬‎)"=>"IR",
    "Iraq (‫العراق‬‎)"=>"IQ",
    "Ireland"=>"IE",
    "Isle of Man"=>"IM",
    "Israel"=>"IL",
    "Italy"=>"IT",
    "Jamaica"=>"JM",
    "Japan (日本)"=>"JP",
    "Jersey"=>"JE",
    "Jordan (‫الأردن‬‎)"=>"JO",
    "Kazakhstan"=>"KZ",
    "Kenya"=>"KE",
    "Kiribati"=>"KI",
    "Kosovo (Kosovë)"=>"XK",
    "Kuwait (‫الكويت‬‎)"=>"KW",
    "Kuwait (‫الكويت‬‎)"=>"KG",
    "Laos (ລາວ)"=>"LA",
    "Latvia"=>"LV",
    "Lebanon (‫لبنان‬‎)"=>"LB",
    "Lesotho"=>"LS",
    "Liberia"=>"LR",
    "Libya (‫ليبيا‬‎)"=>"LY",
    "Liechtenstein"=>"LI",
    "Lithuania"=>"LT",
    "Luxembourg"=>"LU",
    "Macau (澳門)"=>"MO",
    "Macedonia(Македонија"=>"MK",
    "Madagascar"=>"MG",
    "Malawi"=>"MW",
    "Malaysia"=>"MY",
    "Maldives"=>"MV",
    "Mali"=>"ML",
    "Malta"=>"MT",
    "Marshall Islands"=>"MH",
    "Martinique"=>"MQ",
    "Mauritania (‫موريتانيا‬‎)"=>"MR",
    "Mauritius"=>"MU",
    "Mayotte"=>"YT",
    "Mexico"=>"MX",
    "Micronesia"=>"FM",
    "Moldova"=>"MD",
    "Monaco"=>"MC",
    "Mongolia (Монгол)"=>"MN",
    "Montenegro "=>"ME",
    "Montserrat"=>"MS",
    "Morocco (‫المغرب‬‎)"=>"MA",
    "Mozambique "=>"MZ",
    "Myanmar "=>"MM",
    "Namibia "=>"NA",
    "Nauru"=>"NR",
    "Nepal (नेपाल)"=>"NP",
    "Netherlands "=>"NL",
    "New Caledonia ("=>"NC",
    "New Zealand"=>"NZ",
    "Nicaragua"=>"NI",
    "Niger"=>"NE",
    "Nigeria"=>"NG",
    "Niue"=>"NU",
    "Norfolk Island"=>"NF",
    "Northern Mariana Islands"=>"MP",
    "North Korea"=>"KP",
    "Norway"=>"NO",
    "Oman (‫عُمان‬‎)"=>"OM",
    "Pakistan (‫پاکستان‬‎)"=>"PK",
    "Palau"=>"PW",
    "Palestine"=>"PS",
    "Panama "=>"PA",
    "Papua New Guinea"=>"PG",
    "Paraguay"=>"PY",
    "Peru "=>"PE",
    "Philippines"=>"PH",
    "Pitcairn Islands"=>"PN",
    "Poland "=>"PL",
    "Portugal"=>"PT",
    "Puerto Rico"=>"PR",
    "Qatar (‫قطر‬‎)"=>"QA",
    "Réunion"=>"RE",
    "Romania "=>"RO",
    "Russia (Россия)"=>"RU",
    "Rwanda"=>"RW",
    "Saint Barthélemy"=>"BL",
    "Saint Helena"=>"SH",
    "Saint Kitts and Nevis"=>"KN",
    "Saint Lucia"=>"LC",
    "Saint Martin (FRENCH PART)"=>"MF",
    "Saint Pierre and Miquelon"=>"PM",
    "SAINT VINCENT AND THE GRENADINES"=>"VC",
    "Samoa"=>"WS",
    "San Marino"=>"SM",
    "São Tomé and Príncipe"=>"ST",
    "Saudi Arabia (‫المملكة العربية السعودية‬‎)"=>"SA",
    "Senegal "=>"SN",
    "Serbia "=>"RS",
    "Seychelles"=>"SC",
    "Sierra Leone"=>"SL",
    "Singapore"=>"SG",
    "Sint Maarten (DUTCH PART)"=>"SX",
    "Slovakia"=>"SK",
    "Slovenia"=>"SI",
    "Solomon Islands"=>"SB",
    "Somalia "=>"SO",
    "South Africa"=>"ZA",
    "South Georgia & South Sandwich Islands"=>"GS",
    "South Korea (대한민국)"=>"KR",
    "South Sudan (‫جنوب السودان‬‎)"=>"SS",
    "Spain "=>"ES",
    "Sri Lanka"=>"LK",
    "Sudan (‫السودان‬‎)"=>"SD",
    "Suriname"=>"SR",
    "Svalbard and Jan Mayen"=>"SJ",
    "Swaziland"=>"SZ",
    "Sweden "=>"SE",
    "Switzerland "=>"CH",
    "Syria (‫سوريا‬‎)"=>"SY",
    "Taiwan (台灣)"=>"TW",
    "Tajikistan"=>"TJ",
    "Tanzania"=>"TZ",
    "Thailand (ไทย)"=>"TH",
    "Timor-Leste"=>"TL",
    "Togo"=>"TG",
    "Tokelau"=>"TK",
    "Tonga"=>"TO",
    "Trinidad and Tobago"=>"TT",
    "Tristan da Cunha"=>"TA",
    "Tunisia (‫تونس‬‎)"=>"TN",
    "Turkey "=>"TR",
    "Turkmenistan"=>"TM",
    "Turks and Caicos Islands"=>"TC",
    "Tuvalu"=>"TV",
    "Uganda"=>"UG",
    "Ukraine"=>"UA",
    "United Arab Emirates"=>"AE",
    "United Kingdom"=>"GB",
    "UNITED STATES"=>"US",
    "U.S. Outlying Islands"=>"UM",
    "Uruguay"=>"UY",
    "Uzbekistan"=>"UZ",
    "Vanuatu"=>"VU",
    "Vatican City (Città del Vaticano)"=>"VA",
    "Venezuela"=>"VE",
    "Vietnam "=>"VN",
    "VIRGIN ISLANDS, BRITISH"=>"VG",
    "VIRGIN ISLANDS, U.S."=>"VI",
    "Wallis and Futuna"=>"WF",
    "Western Sahara (‫الصحراء الغربية‬‎)"=>"EH",
    "Yemen (‫اليمن‬‎)"=>"YE",
    "Zambia"=>"ZM",
    "Zimbabwe"=>"ZW");
    return $return;
    }
    /**
     * Calculates the offset from UTC for a given timezone
     *
     * @return integer
     */
    function getTimeZoneOffset($timeZone) {
    	$dateTimeZoneUTC = new DateTimeZone("UTC");
    	$dateTimeZoneCurrent = new DateTimeZone($timeZone);
    	$dateTimeUTC = new DateTime("now",$dateTimeZoneUTC);
    	$dateTimeCurrent = new DateTime("now",$dateTimeZoneCurrent);
    	$offset = (($dateTimeZoneCurrent->getOffset($dateTimeUTC))/60);
    	return $offset;
    }
    			

1 个答案:

答案 0 :(得分:1)

我误解了你的问题。

您需要使用javascript来实现目标。将时区发送到服务器后,您需要以JSON格式编码城市数组并将其回送给客户端。然后,客户端将捕获该响应并填充另一个选择字段。

我建议使用jQuery AJAX。