Ok- here's what Im doing:
$dateString = "2015-06-12 06:01 am";
$dtUtc = new \DateTimeZone("UTC");
$dt = \DateTime::createFromFormat('yyyy-mm-dd h:i a', $dateString, $dtUtc);
if ($dt === false) {
die('failed');
}
^ it always fails. why? what am I doing wrong?
Judging by the documentation, you want:
DateTime::createFromFormat('Y-m-d g:i a', $dateString, $dtUtc)
Note that Y
is "4 digit year", m
is "zero-padded month", d
is "zero-padded day", and g
is "zero-padded hour". Admittedly it's quite odd to see a zero-padded hour and an AM/PM designator. If you also want to handle "6:01 am" you would want to go back to h
instead of i
.
See more on this question at Stackoverflow