If the goal is to repair the data because you are tasked with using flawed data, you can use a regular expression to cut off any foul or unneeded AM
or PM
substrings.
Code: (Demo)
$tests = [
"February 13 2022 10:08 PM",
"March 23 2022 00:20 AM",
"April 3 2022 11:20 PM",
"May 28 2022 12:20 AM",
"June 12 2022 21:20 PM",
"July 4 2022 13:20 AM",
];
$tests = preg_replace(
[
'/(?:0\d|1[01]):\d{2}\K AM/', // 00:00 through 11:59 do not need AM
'/(?:2\d|1[3-9]):\d{2}\K [AP]M/', // 13:00 through 23:59 do not need AM or PM
],
'',
$tests
);
var_export($tests);
foreach ($tests as $test) {
echo "\n" . date('Y-m-d H:i:s', strtotime($test));
}
Output:
array (
0 => 'February 13 2022 10:08 PM',
1 => 'March 23 2022 00:20',
2 => 'April 3 2022 11:20 PM',
3 => 'May 28 2022 12:20 AM',
4 => 'June 12 2022 21:20',
5 => 'July 4 2022 13:20',
)
2022-02-13 22:08:00
2022-03-23 00:20:00
2022-04-03 23:20:00
2022-05-28 00:20:00
2022-06-12 21:20:00
2022-07-04 13:20:00