0

I need to subtract time on my WebApp and upload it to the database. so I want to use a 24 hour time so i wont get negative values. so here is my Code
I have a time format like these : 08:08:AM I want to convert these time in 24 hour time format

Time format like these : 08:08:AM
I want to change these time in 24 hour time format.
Thamilhan
  • 13,040
  • 5
  • 37
  • 59

2 Answers2

1

Try this:

$date = DateTime::createFromFormat('h:i A', '08:08 AM');
echo $date->format('H:i:s');   // 08:08:00

$date = DateTime::createFromFormat('h:i A', '08:08 PM');
echo $date->format('H:i:s');  // 20:08:00
Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32
0

If it is 08:08 AM instead of 08:08:AM, this will work for you:

echo date("H:i:s",strtotime("08:08 AM")); //08:08:00
echo date("H:i:s",strtotime("08:08 PM")); //20:08:00

But if it is 08:08:AM, you need to remove that : before AM first and then apply the time:

$time_original = "08:08:AM";
$time = preg_replace("/(\d+):(\d+):(AM|PM)/", "$1:$2 $3", $time_original);
echo date("H:i:s",strtotime($time)); //08:08:00
Thamilhan
  • 13,040
  • 5
  • 37
  • 59