My database has date entries like this: 29th August 20:00 PM

echo $datetest;
//prints 29th August 20:00 PM

$myDateTime = DateTime::createFromFormat('jS F H:i A', $datetest)
$startHoursString = $myDateTime->format('H');

echo $startHoursString;
// prints 08 instead of 20

I was expecting that $startHoursString would be 20 instead of 8.

Could anyone shed some light on this or a workaround?

有帮助吗?

解决方案

It is dirty hack, but it should work, if you are sure that dates are in this AM/PM format, you'll have to remove last 3 characters from the string (AM or PM plus space):

$myDateTime = DateTime::createFromFormat('jS F H:i', substr($datetest, 0, -3));

Fiddle here.

Also, remember that using AM/PM is valid for up to 12 Hour, 20:00 AM/PM is INVALID format.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top