I want to check the date input and convert it to 24h format. For example the datetime is:
$date="2021-02-5 11:45:00 AM"
and after i check it with this condition:
if (preg_match("/am|pm|AM|PM/", $date))
{
$date=date('Y-m-d H:i:s',strtotime("$date"));
}
and my output should be like this however it does not return a date format like that:
$date="2021-02-5 11:45:00";
How can i fix this problem?
You don't need regex
to check if it is 24 hours or 12 hours. you can simply pass the date to strtotime
function to get a timestamp of the date and then format the timestamp with date
function.
$date="2021-02-5 11:45:00 AM";
$date=date('Y-m-j H:i:s',strtotime($date));
echo $date; // 2021-02-5 11:45:00