0

I've been parsing the date with the following code with no problem;

$format = "Ymdhi";

$dateobj = DateTime::createFromFormat($format, $inputDate);
echo $dateobj->getTimestamp();

//This works - returns 1613379600
$dateobj1 = DateTime::createFromFormat($format, "202102150100");
echo $dateobj1->getTimestamp()

//This fails - returns false on createFromFormat and throws a fatal error on getTimestamp
$dateobj2 = DateTime::createFromFormat($format, "202102102105");
echo $dateobj2->getTimestamp()

Anyone know why?

0

1 Answer 1

1

You are using a lowercase h for hours which is 1-12 but you are using 24 hour format for your date. You should use H:

$format = "YmdHi";
$dateobj2 = DateTime::createFromFormat($format, "202102102105");
echo $dateobj2->getTimestamp();

Demo

Not the answer you're looking for? Browse other questions tagged or ask your own question.