문제

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