How to get ISO 3166-1 compatible country code?

16.9k views Asked by At

I am working on displaying list of timezones by country name.

As answered here: Country to timezones in PHP/Zend Framework

I am thinking to call DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, 'US') to list all timezones by country.

Is there any easy way to get list of ISO 3166-1 compatible country codes and their corresponding names in PHP(PHP 5.3)?

5

There are 5 answers

3
Riccardo Zorn On BEST ANSWER

check out the most extensive and up-to-date list I could find:

http earth-info.nima.mil/gns/html/gazetteers2.html (old link see below)

they offer an xls download which contains the following columns:

FIPS      10-4  ISO 3166 digraph    ISO 3166 trigraph   ISO 3166 numeric    TLD IOC SOVEREIGNTY

(check out the 4th sheet in the xls file)

Update 2016: the link is dead, try this: http://geonames.nga.mil/gns/html/

0
Ben Dowling On

You can get a JSON file that has country codes to names at http://country.io/names.json, which you can use to convert country code to a name:

// Either download the JSON in your code like this,
// or just paste the contents into your code
$country_names = json_decode(
    file_get_contents("http://country.io/names.json")
, true);

function get_name($cc) {
    return $country_names[$cc];
}

get_name("GB"); // => "United Kingdom"
1
James On

The official source seems well up to date and has the different ISO 3166 codes available:

Caters for:

  • Alpha 2 Code
  • Alpha 3 Code
  • Numeric

https://www.iso.org/obp/ui/#search/code/

1
alcohol On

I've been maintaining https://github.com/thephpleague/iso3166 for quite a while now. Nothing fancy, just delivers what it advertises :-)

Earlier versions support PHP 5.3, but I dropped that recently since it's completely end-of-life.

0
Krishna Shetty On

I have just compiled a list from the link mentioned by Riccardo Zorn.

$country_name_to_code = array(
    'AFGHANISTAN' => 'AF',
    // 'AKROTIRI' => '*',
    'ALBANIA' => 'AL',
    'ALGERIA' => 'DZ',
    'AMERICAN SAMOA' => 'AS',
    'ANDORRA' => 'AD',
    'ANGOLA' => 'AO',
    'ANGUILLA' => 'AI',
    'ANTARCTICA' => 'AQ',
    'ANTIGUA AND BARBUDA' => 'AG',
    'ARGENTINA' => 'AR',
    'ARMENIA' => 'AM',
    'ARUBA' => 'AW',
    // 'ASHMORE AND CARTIER ISLANDS' => '*',
    'AUSTRALIA' => 'AU',
    'AUSTRIA' => 'AT',
    'AZERBAIJAN' => 'AZ',
    'BAHAMAS, THE' => 'BS',
    'BAHRAIN' => 'BH',
    // 'BAKER ISLAND' => '*',
    'BANGLADESH' => 'BD',
    'BARBADOS' => 'BB',
    // 'BASSAS DA INDIA' => '*',
    'BELARUS' => 'BY',
    'BELGIUM' => 'BE',
    'BELIZE' => 'BZ',
    'BENIN' => 'BJ',
    'BERMUDA' => 'BM',
    'BHUTAN' => 'BT',
    'BOLIVIA' => 'BO',
    'BOSNIA AND HERZEGOVINA' => 'BA',
    'BOTSWANA' => 'BW',
    'BOUVET ISLAND' => 'BV',
    'BRAZIL' => 'BR',
    'BRITISH INDIAN OCEAN TERRITORY' => 'IO',
    'BRUNEI' => 'BN',
    'BULGARIA' => 'BG',
    'BURKINA FASO' => 'BF',
    'BURMA' => 'MM',
    'BURUNDI' => 'BI',
    'CAMBODIA' => 'KH',
    'CAMEROON' => 'CM',
    'CANADA' => 'CA',
    'CAPE VERDE' => 'CV',
    'CAYMAN ISLANDS' => 'KY',
    'CENTRAL AFRICAN REPUBLIC' => 'CF',
    'CHAD' => 'TD',
    'CHILE' => 'CL',
    'CHINA' => 'CN',
    'CHRISTMAS ISLAND' => 'CX',
    // 'CLIPPERTON ISLAND' => '*',
    'COCOS (KEELING) ISLANDS' => 'CC',
    'COLOMBIA' => 'CO',
    'COMOROS' => 'KM',
    'CONGO (BRAZZAVILLE) ' => 'CG',
    'CONGO (KINSHASA)' => 'CD',
    'COOK ISLANDS' => 'CK',
    // 'CORAL SEA ISLANDS' => '*',
    'COSTA RICA' => 'CR',
    'CÔTE D’IVOIRE' => 'CI',
    'CROATIA' => 'HR',
    'CUBA' => 'CU',
    'CURAÇAO' => 'CW',
    'CYPRUS' => 'CY',
    'CZECH REPUBLIC' => 'CZ',
    'DENMARK' => 'DK',
    // 'DHEKELIA' => '*',
    'DJIBOUTI' => 'DJ',
    'DOMINICA' => 'DM',
    'DOMINICAN REPUBLIC' => 'DO',
    'ECUADOR' => 'EC',
    'EGYPT' => 'EG',
    'EL SALVADOR' => 'SV',
    'EQUATORIAL GUINEA' => 'GQ',
    'ERITREA' => 'ER',
    'ESTONIA' => 'EE',
    'ETHIOPIA' => 'ET',
    // 'ETOROFU, HABOMAI, KUNASHIRI, AND SHIKOTAN ISLANDS' => '*',
    // 'EUROPA ISLAND' => '*',
    'FALKLAND ISLANDS (ISLAS MALVINAS)' => 'FK',
    'FAROE ISLANDS' => 'FO',
    'FIJI' => 'FJ',
    'FINLAND' => 'FI',
    'FRANCE' => 'FR',
    'FRENCH GUIANA' => 'GF',
    'FRENCH POLYNESIA' => 'PF',
    'FRENCH SOUTHERN AND ANTARCTIC LANDS' => 'TF',
    'GABON' => 'GA',
    'GAMBIA, THE' => 'GM',
    // 'GAZA STRIP' => '*',
    'GEORGIA' => 'GE',
    'GERMANY' => 'DE',
    'GHANA' => 'GH',
    'GIBRALTAR' => 'GI',
    // 'GLORIOSO ISLANDS' => '*',
    'GREECE' => 'GR',
    'GREENLAND' => 'GL',
    'GRENADA' => 'GD',
    'GUADELOUPE' => 'GP',
    'GUAM' => 'GU',
    'GUATEMALA' => 'GT',
    'GUERNSEY' => 'GG',
    'GUINEA' => 'GN',
    'GUINEA-BISSAU' => 'GW',
    'GUYANA' => 'GY',
    'HAITI' => 'HT',
    'HEARD ISLAND AND MCDONALD ISLANDS' => 'HM',
    'HONDURAS' => 'HN',
    'HONG KONG' => 'HK',
    // 'HOWLAND ISLAND' => '*',
    'HUNGARY' => 'HU',
    'ICELAND' => 'IS',
    'INDIA' => 'IN',
    'INDONESIA' => 'ID',
    'IRAN' => 'IR',
    'IRAQ' => 'IQ',
    'IRELAND' => 'IE',
    'ISLE OF MAN' => 'IM',
    'ISRAEL' => 'IL',
    'ITALY' => 'IT',
    'JAMAICA' => 'JM',
    // 'JAN MAYEN' => '*',
    'JAPAN' => 'JP',
    // 'JARVIS ISLAND' => '*',
    'JERSEY' => 'JE',
    // 'JOHNSTON ATOLL' => '*',
    'JORDAN' => 'JO',
    // 'JUAN DE NOVA ISLAND' => '*',
    'KAZAKHSTAN' => 'KZ',
    'KENYA' => 'KE',
    // 'KINGMAN REEF' => '*',
    'KIRIBATI' => 'KI',
    'KOREA, NORTH' => 'KP',
    'KOREA, SOUTH' => 'KR',
    // 'KOSOVO' => '*',
    'KUWAIT' => 'KW',
    'KYRGYZSTAN' => '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, FEDERATED STATES OF' => 'FM',
    // 'MIDWAY ISLANDS' => '*',
    'MOLDOVA' => 'MD',
    'MONACO' => 'MC',
    'MONGOLIA' => 'MN',
    'MONTENEGRO' => 'ME',
    'MONTSERRAT' => 'MS',
    'MOROCCO' => 'MA',
    'MOZAMBIQUE' => 'MZ',
    'NAMIBIA' => 'NA',
    'NAURU' => 'NR',
    // 'NAVASSA ISLAND' => '*',
    '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',
    'NORWAY' => 'NO',
    'OMAN' => 'OM',
    'PAKISTAN' => 'PK',
    'PALAU' => 'PW',
    // 'PALMYRA ATOLL' => '*',
    'PANAMA' => 'PA',
    'PAPUA NEW GUINEA' => 'PG',
    // 'PARACEL ISLANDS' => '*',
    'PARAGUAY' => 'PY',
    'PERU' => 'PE',
    'PHILIPPINES' => 'PH',
    'PITCAIRN ISLANDS' => 'PN',
    'POLAND' => 'PL',
    'PORTUGAL' => 'PT',
    'PUERTO RICO' => 'PR',
    'QATAR' => 'QA',
    'REUNION' => 'RE',
    'ROMANIA' => 'RO',
    'RUSSIA' => 'RU',
    'RWANDA' => 'RW',
    'SAINT BARTHELEMY' => 'BL',
    'SAINT HELENA, ASCENSION, AND TRISTAN DA CUNHA' => 'SH',
    'SAINT KITTS AND NEVIS' => 'KN',
    'SAINT LUCIA' => 'LC',
    'SAINT MARTIN' => 'MF',
    'SAINT PIERRE AND MIQUELON' => 'PM',
    'SAINT VINCENT AND THE GRENADINES' => 'VC',
    'SAMOA' => 'WS',
    'SAN MARINO' => 'SM',
    'SAO TOME AND PRINCIPE' => 'ST',
    'SAUDI ARABIA' => 'SA',
    'SENEGAL' => 'SN',
    'SERBIA' => 'RS',
    'SEYCHELLES' => 'SC',
    'SIERRA LEONE' => 'SL',
    'SINGAPORE' => 'SG',
    'SINT MAARTEN' => 'SX',
    'SLOVAKIA' => 'SK',
    'SLOVENIA' => 'SI',
    'SOLOMON ISLANDS' => 'SB',
    'SOMALIA' => 'SO',
    'SOUTH AFRICA' => 'ZA',
    'SOUTH GEORGIA AND SOUTH SANDWICH ISLANDS' => 'GS',
    'SOUTH SUDAN' => 'SS',
    'SPAIN' => 'ES',
    // 'SPRATLY ISLANDS' => '*',
    'SRI LANKA' => 'LK',
    'SUDAN' => 'SD',
    'SURINAME' => 'SR',
    // 'SVALBARD' => '*',
    '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',
    // 'TROMELIN ISLAND' => '*',
    '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',
    'URUGUAY' => 'UY',
    'UZBEKISTAN' => 'UZ',
    'VANUATU' => 'VU',
    'VATICAN CITY' => 'VA',
    'VENEZUELA' => 'VE',
    'VIETNAM' => 'VN',
    'VIRGIN ISLANDS, BRITISH' => 'VG',
    'VIRGIN ISLANDS, UNITED STATES ' => 'VI',
    // 'WAKE ISLAND' => '*',
    'WALLIS AND FUTUNA' => 'WF',
    // 'WEST BANK' => '*',
    'WESTERN SAHARA' => 'EH',
    'YEMEN' => 'YE',
    'ZAMBIA' => 'ZM',
    'ZIMBABWE' => 'ZW');

$country_code_to_name = array(
    'AF' => 'Afghanistan',
    'AL' => 'Albania',
    'DZ' => 'Algeria',
    'AS' => 'American Samoa',
    'AD' => 'Andorra',
    'AO' => 'Angola',
    'AI' => 'Anguilla',
    'AQ' => 'Antarctica',
    'AG' => 'Antigua And Barbuda',
    'AR' => 'Argentina',
    'AM' => 'Armenia',
    'AW' => 'Aruba',
    'AU' => 'Australia',
    'AT' => 'Austria',
    'AZ' => 'Azerbaijan',
    'BS' => 'Bahamas, The',
    'BH' => 'Bahrain',
    'BD' => 'Bangladesh',
    'BB' => 'Barbados',
    'BY' => 'Belarus',
    'BE' => 'Belgium',
    'BZ' => 'Belize',
    'BJ' => 'Benin',
    'BM' => 'Bermuda',
    'BT' => 'Bhutan',
    'BO' => 'Bolivia',
    'BA' => 'Bosnia And Herzegovina',
    'BW' => 'Botswana',
    'BV' => 'Bouvet Island',
    'BR' => 'Brazil',
    'IO' => 'British Indian Ocean Territory',
    'BN' => 'Brunei',
    'BG' => 'Bulgaria',
    'BF' => 'Burkina Faso',
    'MM' => 'Burma',
    'BI' => 'Burundi',
    'KH' => 'Cambodia',
    'CM' => 'Cameroon',
    'CA' => 'Canada',
    'CV' => 'Cape Verde',
    'KY' => 'Cayman Islands',
    'CF' => 'Central African Republic',
    'TD' => 'Chad',
    'CL' => 'Chile',
    'CN' => 'China',
    'CX' => 'Christmas Island',
    'CC' => 'Cocos (keeling) Islands',
    'CO' => 'Colombia',
    'KM' => 'Comoros',
    'CG' => 'Congo (brazzaville) ',
    'CD' => 'Congo (kinshasa)',
    'CK' => 'Cook Islands',
    'CR' => 'Costa Rica',
    'CI' => 'CÔte D’ivoire',
    'HR' => 'Croatia',
    'CU' => 'Cuba',
    'CW' => 'CuraÇao',
    'CY' => 'Cyprus',
    'CZ' => 'Czech Republic',
    'DK' => 'Denmark',
    'DJ' => 'Djibouti',
    'DM' => 'Dominica',
    'DO' => 'Dominican Republic',
    'EC' => 'Ecuador',
    'EG' => 'Egypt',
    'SV' => 'El Salvador',
    'GQ' => 'Equatorial Guinea',
    'ER' => 'Eritrea',
    'EE' => 'Estonia',
    'ET' => 'Ethiopia',
    'FK' => 'Falkland Islands (islas Malvinas)',
    'FO' => 'Faroe Islands',
    'FJ' => 'Fiji',
    'FI' => 'Finland',
    'FR' => 'France',
    'GF' => 'French Guiana',
    'PF' => 'French Polynesia',
    'TF' => 'French Southern And Antarctic Lands',
    'GA' => 'Gabon',
    'GM' => 'Gambia, The',
    'GE' => 'Georgia',
    'DE' => 'Germany',
    'GH' => 'Ghana',
    'GI' => 'Gibraltar',
    'GR' => 'Greece',
    'GL' => 'Greenland',
    'GD' => 'Grenada',
    'GP' => 'Guadeloupe',
    'GU' => 'Guam',
    'GT' => 'Guatemala',
    'GG' => 'Guernsey',
    'GN' => 'Guinea',
    'GW' => 'Guinea-bissau',
    'GY' => 'Guyana',
    'HT' => 'Haiti',
    'HM' => 'Heard Island And Mcdonald Islands',
    'HN' => 'Honduras',
    'HK' => 'Hong Kong',
    'HU' => 'Hungary',
    'IS' => 'Iceland',
    'IN' => 'India',
    'ID' => 'Indonesia',
    'IR' => 'Iran',
    'IQ' => 'Iraq',
    'IE' => 'Ireland',
    'IM' => 'Isle Of Man',
    'IL' => 'Israel',
    'IT' => 'Italy',
    'JM' => 'Jamaica',
    'JP' => 'Japan',
    'JE' => 'Jersey',
    'JO' => 'Jordan',
    'KZ' => 'Kazakhstan',
    'KE' => 'Kenya',
    'KI' => 'Kiribati',
    'KP' => 'Korea, North',
    'KR' => 'Korea, South',
    'KW' => 'Kuwait',
    'KG' => 'Kyrgyzstan',
    'LA' => 'Laos',
    'LV' => 'Latvia',
    'LB' => 'Lebanon',
    'LS' => 'Lesotho',
    'LR' => 'Liberia',
    'LY' => 'Libya',
    'LI' => 'Liechtenstein',
    'LT' => 'Lithuania',
    'LU' => 'Luxembourg',
    'MO' => 'Macau',
    'MK' => 'Macedonia',
    'MG' => 'Madagascar',
    'MW' => 'Malawi',
    'MY' => 'Malaysia',
    'MV' => 'Maldives',
    'ML' => 'Mali',
    'MT' => 'Malta',
    'MH' => 'Marshall Islands',
    'MQ' => 'Martinique',
    'MR' => 'Mauritania',
    'MU' => 'Mauritius',
    'YT' => 'Mayotte',
    'MX' => 'Mexico',
    'FM' => 'Micronesia, Federated States Of',
    'MD' => 'Moldova',
    'MC' => 'Monaco',
    'MN' => 'Mongolia',
    'ME' => 'Montenegro',
    'MS' => 'Montserrat',
    'MA' => 'Morocco',
    'MZ' => 'Mozambique',
    'NA' => 'Namibia',
    'NR' => 'Nauru',
    'NP' => 'Nepal',
    'NL' => 'Netherlands',
    'NC' => 'New Caledonia',
    'NZ' => 'New Zealand',
    'NI' => 'Nicaragua',
    'NE' => 'Niger',
    'NG' => 'Nigeria',
    'NU' => 'Niue',
    'NF' => 'Norfolk Island',
    'MP' => 'Northern Mariana Islands',
    'NO' => 'Norway',
    'OM' => 'Oman',
    'PK' => 'Pakistan',
    'PW' => 'Palau',
    'PA' => 'Panama',
    'PG' => 'Papua New Guinea',
    'PY' => 'Paraguay',
    'PE' => 'Peru',
    'PH' => 'Philippines',
    'PN' => 'Pitcairn Islands',
    'PL' => 'Poland',
    'PT' => 'Portugal',
    'PR' => 'Puerto Rico',
    'QA' => 'Qatar',
    'RE' => 'Reunion',
    'RO' => 'Romania',
    'RU' => 'Russia',
    'RW' => 'Rwanda',
    'BL' => 'Saint Barthelemy',
    'SH' => 'Saint Helena, Ascension, And Tristan Da Cunha',
    'KN' => 'Saint Kitts And Nevis',
    'LC' => 'Saint Lucia',
    'MF' => 'Saint Martin',
    'PM' => 'Saint Pierre And Miquelon',
    'VC' => 'Saint Vincent And The Grenadines',
    'WS' => 'Samoa',
    'SM' => 'San Marino',
    'ST' => 'Sao Tome And Principe',
    'SA' => 'Saudi Arabia',
    'SN' => 'Senegal',
    'RS' => 'Serbia',
    'SC' => 'Seychelles',
    'SL' => 'Sierra Leone',
    'SG' => 'Singapore',
    'SX' => 'Sint Maarten',
    'SK' => 'Slovakia',
    'SI' => 'Slovenia',
    'SB' => 'Solomon Islands',
    'SO' => 'Somalia',
    'ZA' => 'South Africa',
    'GS' => 'South Georgia And South Sandwich Islands',
    'SS' => 'South Sudan',
    'ES' => 'Spain',
    'LK' => 'Sri Lanka',
    'SD' => 'Sudan',
    'SR' => 'Suriname',
    'SZ' => 'Swaziland',
    'SE' => 'Sweden',
    'CH' => 'Switzerland',
    'SY' => 'Syria',
    'TW' => 'Taiwan',
    'TJ' => 'Tajikistan',
    'TZ' => 'Tanzania',
    'TH' => 'Thailand',
    'TL' => 'Timor-leste',
    'TG' => 'Togo',
    'TK' => 'Tokelau',
    'TO' => 'Tonga',
    'TT' => 'Trinidad And Tobago',
    'TN' => 'Tunisia',
    'TR' => 'Turkey',
    'TM' => 'Turkmenistan',
    'TC' => 'Turks And Caicos Islands',
    'TV' => 'Tuvalu',
    'UG' => 'Uganda',
    'UA' => 'Ukraine',
    'AE' => 'United Arab Emirates',
    'GB' => 'United Kingdom',
    'US' => 'United States',
    'UY' => 'Uruguay',
    'UZ' => 'Uzbekistan',
    'VU' => 'Vanuatu',
    'VA' => 'Vatican City',
    'VE' => 'Venezuela',
    'VN' => 'Vietnam',
    'VG' => 'Virgin Islands, British',
    'VI' => 'Virgin Islands, United States ',
    'WF' => 'Wallis And Futuna',
    'EH' => 'Western Sahara',
    'YE' => 'Yemen',
    'ZM' => 'Zambia',
    'ZW' => 'Zimbabwe');

It might be useful to others.