PHP - 将时区列表作为数组过滤



在这里回答这个问题之后,我使用@Toland Hon的答案来生成时区列表。列表很大(我得到一个包含 420 个值的数组(。有没有办法将列表的大小减小到仅重要时区?

这是我从一个网站上得到的一个例子,

<option value="American Samoa">(GMT-11:00) American Samoa</option>
<option value="International Date Line West">(GMT-11:00) International Date Line West</option>
<option value="Midway Island">(GMT-11:00) Midway Island</option>
<option value="Hawaii">(GMT-10:00) Hawaii</option>
<option value="Alaska">(GMT-09:00) Alaska</option>
<option value="America/Los_Angeles">(GMT-08:00) America/Los_Angeles</option>
<option value="Pacific Time (US &amp; Canada)">(GMT-08:00) Pacific Time (US &amp; Canada)</option>
<option value="Tijuana">(GMT-08:00) Tijuana</option>
<option value="Arizona">(GMT-07:00) Arizona</option>
<option value="Chihuahua">(GMT-07:00) Chihuahua</option>
<option value="Mazatlan">(GMT-07:00) Mazatlan</option>
<option value="Mountain Time (US &amp; Canada)">(GMT-07:00) Mountain Time (US &amp; Canada)</option>
<option value="Central America">(GMT-06:00) Central America</option>
<option value="Central Time (US &amp; Canada)">(GMT-06:00) Central Time (US &amp; Canada)</option>
<option value="Guadalajara">(GMT-06:00) Guadalajara</option>
<option value="Mexico City">(GMT-06:00) Mexico City</option>
<option value="Monterrey">(GMT-06:00) Monterrey</option>
<option value="Saskatchewan">(GMT-06:00) Saskatchewan</option>
<option value="America/New_York">(GMT-05:00) America/New_York</option>
<option value="America/Toronto">(GMT-05:00) America/Toronto</option>
<option value="Bogota">(GMT-05:00) Bogota</option>
<option value="Eastern Time (US &amp; Canada)">(GMT-05:00) Eastern Time (US &amp; Canada)</option>
<option value="Indiana (East)">(GMT-05:00) Indiana (East)</option>
<option value="Lima">(GMT-05:00) Lima</option>
<option value="Quito">(GMT-05:00) Quito</option>
<option value="Atlantic Time (Canada)">(GMT-04:00) Atlantic Time (Canada)</option>
<option value="Caracas">(GMT-04:00) Caracas</option>
<option value="Georgetown">(GMT-04:00) Georgetown</option>
<option value="La Paz">(GMT-04:00) La Paz</option>
<option value="Santiago">(GMT-04:00) Santiago</option>
<option value="Newfoundland">(GMT-03:30) Newfoundland</option>
<option value="Brasilia">(GMT-03:00) Brasilia</option>
<option value="Buenos Aires">(GMT-03:00) Buenos Aires</option>
<option value="Greenland">(GMT-03:00) Greenland</option>
<option value="Montevideo">(GMT-03:00) Montevideo</option>
<option value="Mid-Atlantic">(GMT-02:00) Mid-Atlantic</option>
<option value="Azores">(GMT-01:00) Azores</option>
<option value="Cape Verde Is.">(GMT-01:00) Cape Verde Is.</option>
<option value="Casablanca">(GMT+00:00) Casablanca</option>
<option value="Dublin">(GMT+00:00) Dublin</option>
<option value="Edinburgh">(GMT+00:00) Edinburgh</option>
<option value="Europe/Lisbon">(GMT+00:00) Europe/Lisbon</option>
<option value="Europe/London">(GMT+00:00) Europe/London</option>
<option value="Lisbon">(GMT+00:00) Lisbon</option>
<option value="London">(GMT+00:00) London</option>
<option value="Monrovia">(GMT+00:00) Monrovia</option>
<option value="UTC">(GMT+00:00) UTC</option>
<option value="Amsterdam">(GMT+01:00) Amsterdam</option>
<option value="Belgrade">(GMT+01:00) Belgrade</option>
<option value="Berlin">(GMT+01:00) Berlin</option>
<option value="Bern">(GMT+01:00) Bern</option>
<option value="Bratislava">(GMT+01:00) Bratislava</option>
<option value="Brussels">(GMT+01:00) Brussels</option>
<option value="Budapest">(GMT+01:00) Budapest</option>
<option value="Copenhagen">(GMT+01:00) Copenhagen</option>
<option value="Ljubljana">(GMT+01:00) Ljubljana</option>
<option value="Madrid">(GMT+01:00) Madrid</option>
<option value="Paris">(GMT+01:00) Paris</option>
<option value="Prague">(GMT+01:00) Prague</option>
<option value="Rome">(GMT+01:00) Rome</option>
<option value="Sarajevo">(GMT+01:00) Sarajevo</option>
<option value="Skopje">(GMT+01:00) Skopje</option>
<option value="Stockholm">(GMT+01:00) Stockholm</option>
<option value="Vienna">(GMT+01:00) Vienna</option>
<option value="Warsaw">(GMT+01:00) Warsaw</option>
<option value="West Central Africa">(GMT+01:00) West Central Africa</option>
<option value="Zagreb">(GMT+01:00) Zagreb</option>
<option value="Athens">(GMT+02:00) Athens</option>
<option value="Bucharest">(GMT+02:00) Bucharest</option>
<option value="Cairo">(GMT+02:00) Cairo</option>
<option value="Harare">(GMT+02:00) Harare</option>
<option value="Helsinki">(GMT+02:00) Helsinki</option>
<option value="Jerusalem">(GMT+02:00) Jerusalem</option>
<option value="Kaliningrad">(GMT+02:00) Kaliningrad</option>
<option value="Kyiv">(GMT+02:00) Kyiv</option>
<option value="Pretoria">(GMT+02:00) Pretoria</option>
<option value="Riga">(GMT+02:00) Riga</option>
<option value="Sofia">(GMT+02:00) Sofia</option>
<option value="Tallinn">(GMT+02:00) Tallinn</option>
<option value="Vilnius">(GMT+02:00) Vilnius</option>
<option value="Baghdad">(GMT+03:00) Baghdad</option>
<option value="Istanbul">(GMT+03:00) Istanbul</option>
<option value="Kuwait">(GMT+03:00) Kuwait</option>
<option value="Minsk">(GMT+03:00) Minsk</option>
<option value="Moscow">(GMT+03:00) Moscow</option>
<option value="Nairobi">(GMT+03:00) Nairobi</option>
<option value="Riyadh">(GMT+03:00) Riyadh</option>
<option value="St. Petersburg">(GMT+03:00) St. Petersburg</option>
<option value="Volgograd">(GMT+03:00) Volgograd</option>
<option value="Tehran">(GMT+03:30) Tehran</option>
<option value="Abu Dhabi">(GMT+04:00) Abu Dhabi</option>
<option value="Baku">(GMT+04:00) Baku</option>
<option value="Muscat">(GMT+04:00) Muscat</option>
<option value="Samara">(GMT+04:00) Samara</option>
<option value="Tbilisi">(GMT+04:00) Tbilisi</option>
<option value="Yerevan">(GMT+04:00) Yerevan</option>
<option value="Kabul">(GMT+04:30) Kabul</option>
<option value="Ekaterinburg">(GMT+05:00) Ekaterinburg</option>
<option value="Islamabad">(GMT+05:00) Islamabad</option>
<option value="Karachi">(GMT+05:00) Karachi</option>
<option value="Tashkent">(GMT+05:00) Tashkent</option>
<option value="Chennai">(GMT+05:30) Chennai</option>
<option value="Kolkata">(GMT+05:30) Kolkata</option>
<option value="Mumbai">(GMT+05:30) Mumbai</option>
<option value="New Delhi">(GMT+05:30) New Delhi</option>
<option value="Sri Jayawardenepura">(GMT+05:30) Sri Jayawardenepura</option>
<option value="Kathmandu">(GMT+05:45) Kathmandu</option>
<option value="Almaty">(GMT+06:00) Almaty</option>
<option value="Astana">(GMT+06:00) Astana</option>
<option value="Dhaka">(GMT+06:00) Dhaka</option>
<option value="Urumqi">(GMT+06:00) Urumqi</option>
<option value="Rangoon">(GMT+06:30) Rangoon</option>
<option value="Bangkok">(GMT+07:00) Bangkok</option>
<option value="Hanoi">(GMT+07:00) Hanoi</option>
<option value="Jakarta">(GMT+07:00) Jakarta</option>
<option value="Krasnoyarsk">(GMT+07:00) Krasnoyarsk</option>
<option value="Novosibirsk">(GMT+07:00) Novosibirsk</option>
<option value="Beijing">(GMT+08:00) Beijing</option>
<option value="Chongqing">(GMT+08:00) Chongqing</option>
<option value="Hong Kong">(GMT+08:00) Hong Kong</option>
<option value="Irkutsk">(GMT+08:00) Irkutsk</option>
<option selected="selected" value="Kuala Lumpur">(GMT+08:00) Kuala Lumpur</option>
<option value="Perth">(GMT+08:00) Perth</option>
<option value="Singapore">(GMT+08:00) Singapore</option>
<option value="Taipei">(GMT+08:00) Taipei</option>
<option value="Ulaanbaatar">(GMT+08:00) Ulaanbaatar</option>
<option value="Osaka">(GMT+09:00) Osaka</option>
<option value="Sapporo">(GMT+09:00) Sapporo</option>
<option value="Seoul">(GMT+09:00) Seoul</option>
<option value="Tokyo">(GMT+09:00) Tokyo</option>
<option value="Yakutsk">(GMT+09:00) Yakutsk</option>
<option value="Adelaide">(GMT+09:30) Adelaide</option>
<option value="Darwin">(GMT+09:30) Darwin</option>
<option value="Brisbane">(GMT+10:00) Brisbane</option>
<option value="Canberra">(GMT+10:00) Canberra</option>
<option value="Guam">(GMT+10:00) Guam</option>
<option value="Hobart">(GMT+10:00) Hobart</option>
<option value="Melbourne">(GMT+10:00) Melbourne</option>
<option value="Port Moresby">(GMT+10:00) Port Moresby</option>
<option value="Sydney">(GMT+10:00) Sydney</option>
<option value="Vladivostok">(GMT+10:00) Vladivostok</option>
<option value="Magadan">(GMT+11:00) Magadan</option>
<option value="New Caledonia">(GMT+11:00) New Caledonia</option>
<option value="Solomon Is.">(GMT+11:00) Solomon Is.</option>
<option value="Srednekolymsk">(GMT+11:00) Srednekolymsk</option>
<option value="Auckland">(GMT+12:00) Auckland</option>
<option value="Fiji">(GMT+12:00) Fiji</option>
<option value="Kamchatka">(GMT+12:00) Kamchatka</option>
<option value="Marshall Is.">(GMT+12:00) Marshall Is.</option>
<option value="Wellington">(GMT+12:00) Wellington</option>
<option value="Chatham Is.">(GMT+12:45) Chatham Is.</option>
<option value="Nuku'alofa">(GMT+13:00) Nuku'alofa</option>
<option value="Samoa">(GMT+13:00) Samoa</option>
<option value="Tokelau Is.">(GMT+13:00) Tokelau Is.</option>

他们只列出了154个时区。从我在StackOverflow中读到的内容来看,使用静态列表并不好,所以我不打算制作静态列表。

如果我使用我需要的时区名称制作一个数组,$timezoneList,并且在向用户显示下拉列表时,我可以使用 $timezoneListDateTimeZone->getOffset 生成一个带有偏移量的适当数组,这是一个好主意吗?

这是我

的解决方案。我有一个带有时区标识符和显示(用于前端(的硬编码数组,我用它来生成具有正确偏移显示的数组。

    static function generateTimeZoneList()
    {
        static $timezone_list;
        if(!isset($timezone_list) || empty($timezone_list)) 
        {
            $identifiers = self::getIdentifiers(); //get identifiers list
            $temp_list = array();
            foreach( $identifiers as $timezone => $display )
            {
                $tz = new DateTimeZone($timezone);
                $temp_list[$timezone] = $tz->getOffset(new DateTime);
            }
            // sort timezone by offset
            asort($temp_list);
            foreach($temp_list as $timezone => $offset)
            {
                $offset_prefix = $offset < 0 ? '-' : '+';
                $offset_formatted = gmdate( 'H:i', abs($offset) );
                $pretty_offset = "GMT${offset_prefix}${offset_formatted}";
                $display = $identifiers[$timezone];
                $timezone_list[$timezone] =  "($pretty_offset) $display";
            }
        }
        return $timezone_list;
    }
    /** 
     * Format: identifier => display name  
     */
   static function getIdentifiers()
    {
       static $identifiers = array(
            "Africa/Cairo" => " Cairo",
            "Africa/Casablanca" => " Casablanca",
            "Africa/Harare" => " Harare",
            "Africa/Johannesburg" => " Pretoria",
            "Africa/Lagos" => " West Central Africa",
            "Africa/Monrovia" => " Monrovia",
            "Africa/Nairobi" => " Nairobi",
            "America/Argentina/Buenos_Aires" => " Buenos Aires",
            "America/Argentina/Buenos_Aires" => " Georgetown",
            "America/Bogota" => " Quito",
            "America/Bogota" => " Bogota",
            "America/Caracas" => " Caracas",
            "America/Chihuahua" => " La Paz",
            "America/Chihuahua" => " Chihuahua",
            "America/Godthab" => " Greenland",
            "America/La_Paz" => " La Paz",
            "America/Lima" => " Lima",
            "America/Los_Angeles" => " Pacific Time (US & Canada)",
            "America/Managua" => " Central America",
            "America/Mazatlan" => " Mazatlan",
            "America/Mexico_City" => " Mexico City",
            "America/Mexico_City" => " Guadalajara",
            "America/Monterrey" => " Monterrey",
            "America/Noronha" => " Mid-Atlantic",
            "America/Santiago" => " Santiago",
            "America/Sao_Paulo" => " Brasilia",
            "America/Tijuana" => " Tijuana",
            "Asia/Almaty" => " Almaty",
            "Asia/Baghdad" => " Baghdad",
            "Asia/Baku" => " Baku",
            "Asia/Bangkok" => " Hanoi",
            "Asia/Bangkok" => " Bangkok",
            "Asia/Calcutta" => " Chennai",
            "Asia/Calcutta" => " Mumbai",
            "Asia/Calcutta" => " New Delhi",
            "Asia/Calcutta" => " Sri Jayawardenepura",
            "Asia/Chongqing" => " Chongqing",
            "Asia/Dhaka" => " Dhaka",
            "Asia/Dhaka" => " Astana",
            "Asia/Hong_Kong" => " Beijing",
            "Asia/Hong_Kong" => " Hong Kong",
            "Asia/Irkutsk" => " Irkutsk",
            "Asia/Jakarta" => " Jakarta",
            "Asia/Jerusalem" => " Jerusalem",
            "Asia/Kabul" => " Kabul",
            "Asia/Kamchatka" => " Kamchatka",
            "Asia/Karachi" => " Karachi",
            "Asia/Karachi" => " Islamabad",
            "Asia/Katmandu" => " Kathmandu",
            "Asia/Kolkata" => " Kolkata",
            "Asia/Krasnoyarsk" => " Krasnoyarsk",
            "Asia/Kuala_Lumpur" => " Kuala Lumpur",
            "Asia/Kuwait" => " Kuwait",
            "Asia/Magadan" => " Solomon Is.",
            "Asia/Magadan" => " Magadan",
            "Asia/Magadan" => " New Caledonia",
            "Asia/Muscat" => " Abu Dhabi",
            "Asia/Muscat" => " Muscat",
            "Asia/Novosibirsk" => " Novosibirsk",
            "Asia/Rangoon" => " Rangoon",
            "Asia/Riyadh" => " Riyadh",
            "Asia/Seoul" => " Seoul",
            "Asia/Singapore" => " Singapore",
            "Asia/Taipei" => " Taipei",
            "Asia/Tashkent" => " Tashkent",
            "Asia/Tbilisi" => " Tbilisi",
            "Asia/Tehran" => " Tehran",
            "Asia/Tokyo" => " Osaka",
            "Asia/Tokyo" => " Tokyo",
            "Asia/Tokyo" => " Sapporo",
            "Asia/Ulan_Bator" => " Ulaan Bataar",
            "Asia/Urumqi" => " Urumqi",
            "Asia/Vladivostok" => " Vladivostok",
            "Asia/Yakutsk" => " Yakutsk",
            "Asia/Yekaterinburg" => " Ekaterinburg",
            "Asia/Yerevan" => " Yerevan",
            "Atlantic/Azores" => " Azores",
            "Atlantic/Cape_Verde" => " Cape Verde Is.",
            "Australia/Adelaide" => " Adelaide",
            "Australia/Brisbane" => " Brisbane",
            "Australia/Canberra" => " Canberra",
            "Australia/Darwin" => " Darwin",
            "Australia/Hobart" => " Hobart",
            "Australia/Melbourne" => " Melbourne",
            "Australia/Perth" => " Perth",
            "Australia/Sydney" => " Sydney",
            "Canada/Atlantic" => " Atlantic Time (Canada)",
            "Canada/Newfoundland" => " Newfoundland",
            "Canada/Saskatchewan" => " Saskatchewan",
            "Etc/Greenwich" => " Greenwich Mean Time : Dublin",
            "Europe/Amsterdam" => " Amsterdam",
            "Europe/Athens" => " Athens",
            "Europe/Belgrade" => " Belgrade",
            "Europe/Berlin" => " Berlin",
            "Europe/Berlin" => " Bern",
            "Europe/Bratislava" => " Bratislava",
            "Europe/Brussels" => " Brussels",
            "Europe/Bucharest" => " Bucharest",
            "Europe/Budapest" => " Budapest",
            "Europe/Copenhagen" => " Copenhagen",
            "Europe/Helsinki" => " Helsinki",
            "Europe/Helsinki" => " Kyiv",
            "Europe/Istanbul" => " Istanbul",
            "Europe/Lisbon" => " Lisbon",
            "Europe/Ljubljana" => " Ljubljana",
            "Europe/London" => " Edinburgh",
            "Europe/London" => " London",
            "Europe/Madrid" => " Madrid",
            "Europe/Minsk" => " Minsk",
            "Europe/Moscow" => " St. Petersburg",
            "Europe/Moscow" => " Moscow",
            "Europe/Paris" => " Paris",
            "Europe/Prague" => " Prague",
            "Europe/Riga" => " Riga",
            "Europe/Rome" => " Rome",
            "Europe/Sarajevo" => " Sarajevo",
            "Europe/Skopje" => " Skopje",
            "Europe/Sofia" => " Sofia",
            "Europe/Stockholm" => " Stockholm",
            "Europe/Tallinn" => " Tallinn",
            "Europe/Vienna" => " Vienna",
            "Europe/Vilnius" => " Vilnius",
            "Europe/Volgograd" => " Volgograd",
            "Europe/Warsaw" => " Warsaw",
            "Europe/Zagreb" => " Zagreb",
            "Pacific/Auckland" => " Wellington",
            "Pacific/Auckland" => " Auckland",
            "Pacific/Fiji" => " Fiji",
            "Pacific/Fiji" => " Marshall Is.",
            "Pacific/Guam" => " Guam",
            "Pacific/Honolulu" => " Hawaii",
            "Pacific/Kwajalein" => " International Date Line West",
            "Pacific/Midway" => " Midway Island",
            "Pacific/Port_Moresby" => " Port Moresby",
            "Pacific/Samoa" => " Samoa",
            "Pacific/Tongatapu" => " Nuku'alofa",
            "US/Alaska" => " Alaska",
            "US/Arizona" => " Arizona",
            "US/Central" => " Central Time (US & Canada)",
            "US/East-Indiana" => " Indiana (East)",
            "US/Eastern" => " Eastern Time (US & Canada)",
            "US/Mountain" => " Mountain Time (US & Canada)",
            "UTC" => " UTC"
        );
        return $identifiers;
    }

结果看起来与此类似,

array (size=121)
  'Pacific/Midway' => string '(GMT-11:00)  Midway Island' (length=26)
  'Pacific/Samoa' => string '(GMT-11:00)  Samoa' (length=18)
  'Pacific/Honolulu' => string '(GMT-10:00)  Hawaii' (length=19)
  'US/Alaska' => string '(GMT-08:00)  Alaska' (length=19)
  'America/Los_Angeles' => string '(GMT-07:00)  Pacific Time (US & Canada)' (length=39)
  'US/Arizona' => string '(GMT-07:00)  Arizona' (length=20)
  'America/Tijuana' => string '(GMT-07:00)  Tijuana' (length=20)
  'Canada/Saskatchewan' => string '(GMT-06:00)  Saskatchewan' (length=25)
  'America/Managua' => string '(GMT-06:00)  Central America' (length=28)
  'America/Mazatlan' => string '(GMT-06:00)  Mazatlan' (length=21)
  'America/Chihuahua' => string '(GMT-06:00)  Chihuahua' (length=22)
  'US/Mountain' => string '(GMT-06:00)  Mountain Time (US & Canada)' (length=40)
  'America/Lima' => string '(GMT-05:00)  Lima' (length=17)

相关内容

  • 没有找到相关文章

最新更新