1

I have the following date: (Tue 7:00AM EST).

Now in order to change its format to 2014-01-21 7:00, I made use of DateTime:createFromFormat() and DateTime::format(). However, it does not seem to find the pattern in the string mentioned above.

Here's the code:

<?php

$date = DateTime::createFromFormat('(D g:iA T)', '(Tue 7:00AM EST)');
if($date !== false) {
    var_dump($date->format('Y-m-d H:i'));
} else {
    echo 'Invalid date!';
}

?>

Prints: Invalid Date!
2
  • 3
    Have you tried trimming out the brackets? Commented Jan 21, 2014 at 14:21
  • 1
    eval.in/92935 Commented Jan 21, 2014 at 14:45

3 Answers 3

1

Trim out the brackets:

$date = DateTime::createFromFormat('D g:iA T', trim('(Tue 7:00AM EST)', '()'));
Sign up to request clarification or add additional context in comments.

Comments

0

print_r(DateTime::getLastErrors(),1); gave me:

Array ( [warning_count] => 0 [warnings] => Array ( )

[error_count] => 1
[errors] => Array
    (
        [16] => Data missing
    )

)

which means at index 16 of the string '(Tue 7:00AM EST)' there is data missing (at the last bracket).

echo print_r(DateTimeZone::listIdentifiers()); lists the following timezones

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/Fortaleza
[103] => America/Glace_Bay
[104] => America/Godthab
[105] => America/Goose_Bay
[106] => America/Grand_Turk
[107] => America/Grenada
[108] => America/Guadeloupe
[109] => America/Guatemala
[110] => America/Guayaquil
[111] => America/Guyana
[112] => America/Halifax
[113] => America/Havana
[114] => America/Hermosillo
[115] => America/Indiana/Indianapolis
[116] => America/Indiana/Knox
[117] => America/Indiana/Marengo
[118] => America/Indiana/Petersburg
[119] => America/Indiana/Tell_City
[120] => America/Indiana/Vevay
[121] => America/Indiana/Vincennes
[122] => America/Indiana/Winamac
[123] => America/Inuvik
[124] => America/Iqaluit
[125] => America/Jamaica
[126] => America/Juneau
[127] => America/Kentucky/Louisville
[128] => America/Kentucky/Monticello
[129] => America/Kralendijk
[130] => America/La_Paz
[131] => America/Lima
[132] => America/Los_Angeles
[133] => America/Lower_Princes
[134] => America/Maceio
[135] => America/Managua
[136] => America/Manaus
[137] => America/Marigot
[138] => America/Martinique
[139] => America/Matamoros
[140] => America/Mazatlan
[141] => America/Menominee
[142] => America/Merida
[143] => America/Metlakatla
[144] => America/Mexico_City
[145] => America/Miquelon
[146] => America/Moncton
[147] => America/Monterrey
[148] => America/Montevideo
[149] => America/Montserrat
[150] => America/Nassau
[151] => America/New_York
[152] => America/Nipigon
[153] => America/Nome
[154] => America/Noronha
[155] => America/North_Dakota/Beulah
[156] => America/North_Dakota/Center
[157] => America/North_Dakota/New_Salem
[158] => America/Ojinaga
[159] => America/Panama
[160] => America/Pangnirtung
[161] => America/Paramaribo
[162] => America/Phoenix
[163] => America/Port-au-Prince
[164] => America/Port_of_Spain
[165] => America/Porto_Velho
[166] => America/Puerto_Rico
[167] => America/Rainy_River
[168] => America/Rankin_Inlet
[169] => America/Recife
[170] => America/Regina
[171] => America/Resolute
[172] => America/Rio_Branco
[173] => America/Santa_Isabel
[174] => America/Santarem
[175] => America/Santiago
[176] => America/Santo_Domingo
[177] => America/Sao_Paulo
[178] => America/Scoresbysund
[179] => America/Sitka
[180] => America/St_Barthelemy
[181] => America/St_Johns
[182] => America/St_Kitts
[183] => America/St_Lucia
[184] => America/St_Thomas
[185] => America/St_Vincent
[186] => America/Swift_Current
[187] => America/Tegucigalpa
[188] => America/Thule
[189] => America/Thunder_Bay
[190] => America/Tijuana
[191] => America/Toronto
[192] => America/Tortola
[193] => America/Vancouver
[194] => America/Whitehorse
[195] => America/Winnipeg
[196] => America/Yakutat
[197] => America/Yellowknife
[198] => Antarctica/Casey
[199] => Antarctica/Davis
[200] => Antarctica/DumontDUrville
[201] => Antarctica/Macquarie
[202] => Antarctica/Mawson
[203] => Antarctica/McMurdo
[204] => Antarctica/Palmer
[205] => Antarctica/Rothera
[206] => Antarctica/Syowa
[207] => Antarctica/Vostok
[208] => Arctic/Longyearbyen
[209] => Asia/Aden
[210] => Asia/Almaty
[211] => Asia/Amman
[212] => Asia/Anadyr
[213] => Asia/Aqtau
[214] => Asia/Aqtobe
[215] => Asia/Ashgabat
[216] => Asia/Baghdad
[217] => Asia/Bahrain
[218] => Asia/Baku
[219] => Asia/Bangkok
[220] => Asia/Beirut
[221] => Asia/Bishkek
[222] => Asia/Brunei
[223] => Asia/Choibalsan
[224] => Asia/Chongqing
[225] => Asia/Colombo
[226] => Asia/Damascus
[227] => Asia/Dhaka
[228] => Asia/Dili
[229] => Asia/Dubai
[230] => Asia/Dushanbe
[231] => Asia/Gaza
[232] => Asia/Harbin
[233] => Asia/Hebron
[234] => Asia/Ho_Chi_Minh
[235] => Asia/Hong_Kong
[236] => Asia/Hovd
[237] => Asia/Irkutsk
[238] => Asia/Jakarta
[239] => Asia/Jayapura
[240] => Asia/Jerusalem
[241] => Asia/Kabul
[242] => Asia/Kamchatka
[243] => Asia/Karachi
[244] => Asia/Kashgar
[245] => Asia/Kathmandu
[246] => Asia/Khandyga
[247] => Asia/Kolkata
[248] => Asia/Krasnoyarsk
[249] => Asia/Kuala_Lumpur
[250] => Asia/Kuching
[251] => Asia/Kuwait
[252] => Asia/Macau
[253] => Asia/Magadan
[254] => Asia/Makassar
[255] => Asia/Manila
[256] => Asia/Muscat
[257] => Asia/Nicosia
[258] => Asia/Novokuznetsk
[259] => Asia/Novosibirsk
[260] => Asia/Omsk
[261] => Asia/Oral
[262] => Asia/Phnom_Penh
[263] => Asia/Pontianak
[264] => Asia/Pyongyang
[265] => Asia/Qatar
[266] => Asia/Qyzylorda
[267] => Asia/Rangoon
[268] => Asia/Riyadh
[269] => Asia/Sakhalin
[270] => Asia/Samarkand
[271] => Asia/Seoul
[272] => Asia/Shanghai
[273] => Asia/Singapore
[274] => Asia/Taipei
[275] => Asia/Tashkent
[276] => Asia/Tbilisi
[277] => Asia/Tehran
[278] => Asia/Thimphu
[279] => Asia/Tokyo
[280] => Asia/Ulaanbaatar
[281] => Asia/Urumqi
[282] => Asia/Ust-Nera
[283] => Asia/Vientiane
[284] => Asia/Vladivostok
[285] => Asia/Yakutsk
[286] => Asia/Yekaterinburg
[287] => Asia/Yerevan
[288] => Atlantic/Azores
[289] => Atlantic/Bermuda
[290] => Atlantic/Canary
[291] => Atlantic/Cape_Verde
[292] => Atlantic/Faroe
[293] => Atlantic/Madeira
[294] => Atlantic/Reykjavik
[295] => Atlantic/South_Georgia
[296] => Atlantic/St_Helena
[297] => Atlantic/Stanley
[298] => Australia/Adelaide
[299] => Australia/Brisbane
[300] => Australia/Broken_Hill
[301] => Australia/Currie
[302] => Australia/Darwin
[303] => Australia/Eucla
[304] => Australia/Hobart
[305] => Australia/Lindeman
[306] => Australia/Lord_Howe
[307] => Australia/Melbourne
[308] => Australia/Perth
[309] => Australia/Sydney
[310] => Europe/Amsterdam
[311] => Europe/Andorra
[312] => Europe/Athens
[313] => Europe/Belgrade
[314] => Europe/Berlin
[315] => Europe/Bratislava
[316] => Europe/Brussels
[317] => Europe/Bucharest
[318] => Europe/Budapest
[319] => Europe/Busingen
[320] => Europe/Chisinau
[321] => Europe/Copenhagen
[322] => Europe/Dublin
[323] => Europe/Gibraltar
[324] => Europe/Guernsey
[325] => Europe/Helsinki
[326] => Europe/Isle_of_Man
[327] => Europe/Istanbul
[328] => Europe/Jersey
[329] => Europe/Kaliningrad
[330] => Europe/Kiev
[331] => Europe/Lisbon
[332] => Europe/Ljubljana
[333] => Europe/London
[334] => Europe/Luxembourg
[335] => Europe/Madrid
[336] => Europe/Malta
[337] => Europe/Mariehamn
[338] => Europe/Minsk
[339] => Europe/Monaco
[340] => Europe/Moscow
[341] => Europe/Oslo
[342] => Europe/Paris
[343] => Europe/Podgorica
[344] => Europe/Prague
[345] => Europe/Riga
[346] => Europe/Rome
[347] => Europe/Samara
[348] => Europe/San_Marino
[349] => Europe/Sarajevo
[350] => Europe/Simferopol
[351] => Europe/Skopje
[352] => Europe/Sofia
[353] => Europe/Stockholm
[354] => Europe/Tallinn
[355] => Europe/Tirane
[356] => Europe/Uzhgorod
[357] => Europe/Vaduz
[358] => Europe/Vatican
[359] => Europe/Vienna
[360] => Europe/Vilnius
[361] => Europe/Volgograd
[362] => Europe/Warsaw
[363] => Europe/Zagreb
[364] => Europe/Zaporozhye
[365] => Europe/Zurich
[366] => Indian/Antananarivo
[367] => Indian/Chagos
[368] => Indian/Christmas
[369] => Indian/Cocos
[370] => Indian/Comoro
[371] => Indian/Kerguelen
[372] => Indian/Mahe
[373] => Indian/Maldives
[374] => Indian/Mauritius
[375] => Indian/Mayotte
[376] => Indian/Reunion
[377] => Pacific/Apia
[378] => Pacific/Auckland
[379] => Pacific/Chatham
[380] => Pacific/Chuuk
[381] => Pacific/Easter
[382] => Pacific/Efate
[383] => Pacific/Enderbury
[384] => Pacific/Fakaofo
[385] => Pacific/Fiji
[386] => Pacific/Funafuti
[387] => Pacific/Galapagos
[388] => Pacific/Gambier
[389] => Pacific/Guadalcanal
[390] => Pacific/Guam
[391] => Pacific/Honolulu
[392] => Pacific/Johnston
[393] => Pacific/Kiritimati
[394] => Pacific/Kosrae
[395] => Pacific/Kwajalein
[396] => Pacific/Majuro
[397] => Pacific/Marquesas
[398] => Pacific/Midway
[399] => Pacific/Nauru
[400] => Pacific/Niue
[401] => Pacific/Norfolk
[402] => Pacific/Noumea
[403] => Pacific/Pago_Pago
[404] => Pacific/Palau
[405] => Pacific/Pitcairn
[406] => Pacific/Pohnpei
[407] => Pacific/Port_Moresby
[408] => Pacific/Rarotonga
[409] => Pacific/Saipan
[410] => Pacific/Tahiti
[411] => Pacific/Tarawa
[412] => Pacific/Tongatapu
[413] => Pacific/Wake
[414] => Pacific/Wallis
[415] => UTC
)

So there is not EST listed. However $date = DateTime::createFromFormat('T', 'EST'); works without problems, in contrast to $date = DateTime::createFromFormat('(T)', '(EST)');.

I could not manage to get exactly that example working, but maybe my mentioned thoughts can help you.

BTW: The following worked for me:

  • Removing the brackets (maybe the easiest solution)
  • $date = DateTime::createFromFormat('(D g:iA)', '(Tue 7:00AM)'); (without timezone)

1 Comment

EST is supported, demo. If timezone wouldn't be supported, you would get error: The timezone could not be found in the database, like this demo.
0

You don't need to trim out the brackets, your date format can be parsed by \DateTime::createFromFormat() without any problems:-

$dateStr = "(Tue 7:00AM EST)";
$date = \DateTime::createFromFormat("(D g:iA T", $dateStr);
var_dump($date);

You will notice that the trick is to leave out the trailing ).

Output:-

object(DateTime)[1]
  public 'date' => string '2014-01-21 07:00:00' (length=19)
  public 'timezone_type' => int 2
  public 'timezone' => string 'EST' (length=3)

See it working and the manual for formats accepted by DateTime::createFromFormat().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.