How to Convert Date & Time from One Timezone to Another Using PHP

As an application user, they may wish to see the date and time in their own time zone. For that, the best thing is to save time on the database in the general format like UTC/GMT and then convert it back to the required user timezone. In PHP, it’s extremely simple to convert a date and time from one time zone to another. And here is the quick blog which will help you to convert the date to different timezone in PHP.

Convert DateTime from One Timezone to Another

Example code to convert date and time from one timezone to another,

<?php
function convertDateTime($time = '', $fromTimezone = '', $toTimezone = '')
{
	$date = new DateTime($time, new DateTimeZone($fromTimezone));
	$date->setTimezone(new DateTimeZone($toTimezone));
	return $date->format('Y-m-d H:i:s');
}

$date = '2020-04-11 12:00:00';

echo 'UTC Time: ' . $date . '<br>';

echo 'EST Time: ' . convertDateTime($date, 'UTC', 'EST') . '<br>';

echo 'PST Time: ' . convertDateTime($date, 'UTC', 'PST') . '<br>';

echo 'IST Time: ' . convertDateTime($date, 'UTC', 'IST');

In the above code,

  • Create a custom function named convertDateTime with three parameters ($time: the time which is going to convert, $fromTimezone and $toTimezone).
  • Within the custom function, create a DateTime object using the given input time as a first parameter and the original time zone as a second parameter.
  • Then convert the time to the desired timezone using the setTimezone method.
  • Finally, return the converted date in any of our desired date format.

The output of the above code will look as follows,

UTC Time: 2020-04-11 08:30:09
EST Time: 2020-04-11 03:30:09
PST Time: 2020-04-11 00:30:09
IST Time: 2020-04-11 10:30:09

Get All Timezone Identifiers

In case you want to get the list of all the time zone identifiers, use the PHP inbuilt function timezone_identifiers_list. It is used to return an indexed array containing all the timezone identifiers.

<?php
$zones = timezone_identifiers_list();
echo '<pre>'; 
print_r($zones);

The output will look as follows,

Array
(
[0] => Africa/Abidjan
[1] => Africa/Accra
[2] => Africa/Addis_Ababa
[3] => Africa/Algiers
[4] => Africa/Asmara
[5] => Africa/Bamako
[6] => Africa/Bangui
[7] => Africa/Banjul
[8] => Africa/Bissau
[9] => Africa/Blantyre
[10] => Africa/Brazzaville
[11] => Africa/Bujumbura
[12] => Africa/Cairo
[13] => Africa/Casablanca
[14] => Africa/Ceuta
[15] => Africa/Conakry
[16] => Africa/Dakar
[17] => Africa/Dar_es_Salaam
[18] => Africa/Djibouti
[19] => Africa/Douala
[20] => Africa/El_Aaiun
[21] => Africa/Freetown
[22] => Africa/Gaborone
[23] => Africa/Harare
[24] => Africa/Johannesburg
[25] => Africa/Juba
[26] => Africa/Kampala
[27] => Africa/Khartoum
[28] => Africa/Kigali
[29] => Africa/Kinshasa
[30] => Africa/Lagos
[31] => Africa/Libreville
[32] => Africa/Lome
[33] => Africa/Luanda
[34] => Africa/Lubumbashi
[35] => Africa/Lusaka
[36] => Africa/Malabo
[37] => Africa/Maputo
[38] => Africa/Maseru
[39] => Africa/Mbabane
[40] => Africa/Mogadishu
[41] => Africa/Monrovia
[42] => Africa/Nairobi
[43] => Africa/Ndjamena
[44] => Africa/Niamey
[45] => Africa/Nouakchott
[46] => Africa/Ouagadougou
[47] => Africa/Porto-Novo
[48] => Africa/Sao_Tome
[49] => Africa/Tripoli
[50] => Africa/Tunis
[51] => Africa/Windhoek
[52] => America/Adak
[53] => America/Anchorage
[54] => America/Anguilla
[55] => America/Antigua
[56] => America/Araguaina
[57] => America/Argentina/Buenos_Aires
[58] => America/Argentina/Catamarca
[59] => America/Argentina/Cordoba
[60] => America/Argentina/Jujuy
[61] => America/Argentina/La_Rioja
[62] => America/Argentina/Mendoza
[63] => America/Argentina/Rio_Gallegos
[64] => America/Argentina/Salta
[65] => America/Argentina/San_Juan
[66] => America/Argentina/San_Luis
[67] => America/Argentina/Tucuman
[68] => America/Argentina/Ushuaia
[69] => America/Aruba
[70] => America/Asuncion
[71] => America/Atikokan
[72] => America/Bahia
[73] => America/Bahia_Banderas
[74] => America/Barbados
[75] => America/Belem
[76] => America/Belize
[77] => America/Blanc-Sablon
[78] => America/Boa_Vista
[79] => America/Bogota
[80] => America/Boise
[81] => America/Cambridge_Bay
[82] => America/Campo_Grande
[83] => America/Cancun
[84] => America/Caracas
[85] => America/Cayenne
[86] => America/Cayman
[87] => America/Chicago
[88] => America/Chihuahua
[89] => America/Costa_Rica
[90] => America/Creston
[91] => America/Cuiaba
[92] => America/Curacao
[93] => America/Danmarkshavn
[94] => America/Dawson
[95] => America/Dawson_Creek
[96] => America/Denver
[97] => America/Detroit
[98] => America/Dominica
[99] => America/Edmonton
[100] => America/Eirunepe
[101] => America/El_Salvador
[102] => America/Fort_Nelson
[103] => America/Fortaleza
[104] => America/Glace_Bay
[105] => America/Godthab
[106] => America/Goose_Bay
[107] => America/Grand_Turk
[108] => America/Grenada
[109] => America/Guadeloupe
[110] => America/Guatemala
[111] => America/Guayaquil
[112] => America/Guyana
[113] => America/Halifax
[114] => America/Havana
[115] => America/Hermosillo
[116] => America/Indiana/Indianapolis
[117] => America/Indiana/Knox
[118] => America/Indiana/Marengo
[119] => America/Indiana/Petersburg
[120] => America/Indiana/Tell_City
[121] => America/Indiana/Vevay
[122] => America/Indiana/Vincennes
[123] => America/Indiana/Winamac
[124] => America/Inuvik
[125] => America/Iqaluit
[126] => America/Jamaica
[127] => America/Juneau
[128] => America/Kentucky/Louisville
[129] => America/Kentucky/Monticello
[130] => America/Kralendijk
[131] => America/La_Paz
[132] => America/Lima
[133] => America/Los_Angeles
[134] => America/Lower_Princes
[135] => America/Maceio
[136] => America/Managua
[137] => America/Manaus
[138] => America/Marigot
[139] => America/Martinique
[140] => America/Matamoros
[141] => America/Mazatlan
[142] => America/Menominee
[143] => America/Merida
[144] => America/Metlakatla
[145] => America/Mexico_City
[146] => America/Miquelon
[147] => America/Moncton
[148] => America/Monterrey
[149] => America/Montevideo
[150] => America/Montserrat
[151] => America/Nassau
[152] => America/New_York
[153] => America/Nipigon
[154] => America/Nome
[155] => America/Noronha
[156] => America/North_Dakota/Beulah
[157] => America/North_Dakota/Center
[158] => America/North_Dakota/New_Salem
[159] => America/Ojinaga
[160] => America/Panama
[161] => America/Pangnirtung
[162] => America/Paramaribo
[163] => America/Phoenix
[164] => America/Port-au-Prince
[165] => America/Port_of_Spain
[166] => America/Porto_Velho
[167] => America/Puerto_Rico
[168] => America/Punta_Arenas
[169] => America/Rainy_River
[170] => America/Rankin_Inlet
[171] => America/Recife
[172] => America/Regina
[173] => America/Resolute
[174] => America/Rio_Branco
[175] => America/Santarem
[176] => America/Santiago
[177] => America/Santo_Domingo
[178] => America/Sao_Paulo
[179] => America/Scoresbysund
[180] => America/Sitka
[181] => America/St_Barthelemy
[182] => America/St_Johns
[183] => America/St_Kitts
[184] => America/St_Lucia
[185] => America/St_Thomas
[186] => America/St_Vincent
[187] => America/Swift_Current
[188] => America/Tegucigalpa
[189] => America/Thule
[190] => America/Thunder_Bay
[191] => America/Tijuana
[192] => America/Toronto
[193] => America/Tortola
[194] => America/Vancouver
[195] => America/Whitehorse
[196] => America/Winnipeg
[197] => America/Yakutat
[198] => America/Yellowknife
[199] => Antarctica/Casey
[200] => Antarctica/Davis
[201] => Antarctica/DumontDUrville
[202] => Antarctica/Macquarie
[203] => Antarctica/Mawson
[204] => Antarctica/McMurdo
[205] => Antarctica/Palmer
[206] => Antarctica/Rothera
[207] => Antarctica/Syowa
[208] => Antarctica/Troll
[209] => Antarctica/Vostok
[210] => Arctic/Longyearbyen
[211] => Asia/Aden
[212] => Asia/Almaty
[213] => Asia/Amman
[214] => Asia/Anadyr
[215] => Asia/Aqtau
[216] => Asia/Aqtobe
[217] => Asia/Ashgabat
[218] => Asia/Atyrau
[219] => Asia/Baghdad
[220] => Asia/Bahrain
[221] => Asia/Baku
[222] => Asia/Bangkok
[223] => Asia/Barnaul
[224] => Asia/Beirut
[225] => Asia/Bishkek
[226] => Asia/Brunei
[227] => Asia/Chita
[228] => Asia/Choibalsan
[229] => Asia/Colombo
[230] => Asia/Damascus
[231] => Asia/Dhaka
[232] => Asia/Dili
[233] => Asia/Dubai
[234] => Asia/Dushanbe
[235] => Asia/Famagusta
[236] => Asia/Gaza
[237] => Asia/Hebron
[238] => Asia/Ho_Chi_Minh
[239] => Asia/Hong_Kong
[240] => Asia/Hovd
[241] => Asia/Irkutsk
[242] => Asia/Jakarta
[243] => Asia/Jayapura
[244] => Asia/Jerusalem
[245] => Asia/Kabul
[246] => Asia/Kamchatka
[247] => Asia/Karachi
[248] => Asia/Kathmandu
[249] => Asia/Khandyga
[250] => Asia/Kolkata
[251] => Asia/Krasnoyarsk
[252] => Asia/Kuala_Lumpur
[253] => Asia/Kuching
[254] => Asia/Kuwait
[255] => Asia/Macau
[256] => Asia/Magadan
[257] => Asia/Makassar
[258] => Asia/Manila
[259] => Asia/Muscat
[260] => Asia/Nicosia
[261] => Asia/Novokuznetsk
[262] => Asia/Novosibirsk
[263] => Asia/Omsk
[264] => Asia/Oral
[265] => Asia/Phnom_Penh
[266] => Asia/Pontianak
[267] => Asia/Pyongyang
[268] => Asia/Qatar
[269] => Asia/Qostanay
[270] => Asia/Qyzylorda
[271] => Asia/Riyadh
[272] => Asia/Sakhalin
[273] => Asia/Samarkand
[274] => Asia/Seoul
[275] => Asia/Shanghai
[276] => Asia/Singapore
[277] => Asia/Srednekolymsk
[278] => Asia/Taipei
[279] => Asia/Tashkent
[280] => Asia/Tbilisi
[281] => Asia/Tehran
[282] => Asia/Thimphu
[283] => Asia/Tokyo
[284] => Asia/Tomsk
[285] => Asia/Ulaanbaatar
[286] => Asia/Urumqi
[287] => Asia/Ust-Nera
[288] => Asia/Vientiane
[289] => Asia/Vladivostok
[290] => Asia/Yakutsk
[291] => Asia/Yangon
[292] => Asia/Yekaterinburg
[293] => Asia/Yerevan
[294] => Atlantic/Azores
[295] => Atlantic/Bermuda
[296] => Atlantic/Canary
[297] => Atlantic/Cape_Verde
[298] => Atlantic/Faroe
[299] => Atlantic/Madeira
[300] => Atlantic/Reykjavik
[301] => Atlantic/South_Georgia
[302] => Atlantic/St_Helena
[303] => Atlantic/Stanley
[304] => Australia/Adelaide
[305] => Australia/Brisbane
[306] => Australia/Broken_Hill
[307] => Australia/Currie
[308] => Australia/Darwin
[309] => Australia/Eucla
[310] => Australia/Hobart
[311] => Australia/Lindeman
[312] => Australia/Lord_Howe
[313] => Australia/Melbourne
[314] => Australia/Perth
[315] => Australia/Sydney
[316] => Europe/Amsterdam
[317] => Europe/Andorra
[318] => Europe/Astrakhan
[319] => Europe/Athens
[320] => Europe/Belgrade
[321] => Europe/Berlin
[322] => Europe/Bratislava
[323] => Europe/Brussels
[324] => Europe/Bucharest
[325] => Europe/Budapest
[326] => Europe/Busingen
[327] => Europe/Chisinau
[328] => Europe/Copenhagen
[329] => Europe/Dublin
[330] => Europe/Gibraltar
[331] => Europe/Guernsey
[332] => Europe/Helsinki
[333] => Europe/Isle_of_Man
[334] => Europe/Istanbul
[335] => Europe/Jersey
[336] => Europe/Kaliningrad
[337] => Europe/Kiev
[338] => Europe/Kirov
[339] => Europe/Lisbon
[340] => Europe/Ljubljana
[341] => Europe/London
[342] => Europe/Luxembourg
[343] => Europe/Madrid
[344] => Europe/Malta
[345] => Europe/Mariehamn
[346] => Europe/Minsk
[347] => Europe/Monaco
[348] => Europe/Moscow
[349] => Europe/Oslo
[350] => Europe/Paris
[351] => Europe/Podgorica
[352] => Europe/Prague
[353] => Europe/Riga
[354] => Europe/Rome
[355] => Europe/Samara
[356] => Europe/San_Marino
[357] => Europe/Sarajevo
[358] => Europe/Saratov
[359] => Europe/Simferopol
[360] => Europe/Skopje
[361] => Europe/Sofia
[362] => Europe/Stockholm
[363] => Europe/Tallinn
[364] => Europe/Tirane
[365] => Europe/Ulyanovsk
[366] => Europe/Uzhgorod
[367] => Europe/Vaduz
[368] => Europe/Vatican
[369] => Europe/Vienna
[370] => Europe/Vilnius
[371] => Europe/Volgograd
[372] => Europe/Warsaw
[373] => Europe/Zagreb
[374] => Europe/Zaporozhye
[375] => Europe/Zurich
[376] => Indian/Antananarivo
[377] => Indian/Chagos
[378] => Indian/Christmas
[379] => Indian/Cocos
[380] => Indian/Comoro
[381] => Indian/Kerguelen
[382] => Indian/Mahe
[383] => Indian/Maldives
[384] => Indian/Mauritius
[385] => Indian/Mayotte
[386] => Indian/Reunion
[387] => Pacific/Apia
[388] => Pacific/Auckland
[389] => Pacific/Bougainville
[390] => Pacific/Chatham
[391] => Pacific/Chuuk
[392] => Pacific/Easter
[393] => Pacific/Efate
[394] => Pacific/Enderbury
[395] => Pacific/Fakaofo
[396] => Pacific/Fiji
[397] => Pacific/Funafuti
[398] => Pacific/Galapagos
[399] => Pacific/Gambier
[400] => Pacific/Guadalcanal
[401] => Pacific/Guam
[402] => Pacific/Honolulu
[403] => Pacific/Kiritimati
[404] => Pacific/Kosrae
[405] => Pacific/Kwajalein
[406] => Pacific/Majuro
[407] => Pacific/Marquesas
[408] => Pacific/Midway
[409] => Pacific/Nauru
[410] => Pacific/Niue
[411] => Pacific/Norfolk
[412] => Pacific/Noumea
[413] => Pacific/Pago_Pago
[414] => Pacific/Palau
[415] => Pacific/Pitcairn
[416] => Pacific/Pohnpei
[417] => Pacific/Port_Moresby
[418] => Pacific/Rarotonga
[419] => Pacific/Saipan
[420] => Pacific/Tahiti
[421] => Pacific/Tarawa
[422] => Pacific/Tongatapu
[423] => Pacific/Wake
[424] => Pacific/Wallis
[425] => UTC
)

You can use the above timezone identifiers in our code as follows,

<?php
function convertDateTime($time = '', $fromTimezone = '', $toTimezone = '')
{
	$date = new DateTime($time, new DateTimeZone($fromTimezone));
	$date->setTimezone(new DateTimeZone($toTimezone));
	return $date->format('Y-m-d H:i:s');
}

$date = '2020-04-11 12:00:00';

echo 'UTC Time: ' . $date;

echo '<br>';

echo 'Converted Time: ' . convertDateTime($date, 'UTC', 'Pacific/Chatham');

The output will look as follows,

UTC Time: 2020-04-11 12:00:00
Converted Time: 2020-04-12 00:45:00

Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *