How do you convert between 12 hour time and 24 hour time in PHP?

Php

Php Problem Overview


I have a time as a string, for example:

$time = '5:36 pm';

I require this to be in 24 hour format (i.e. 17:36), however I don't know which functions I need to use to convert it. Please help.

Php Solutions


Solution 1 - Php

// 24-hour time to 12-hour time 
$time_in_12_hour_format  = date("g:i a", strtotime("13:30"));
 
// 12-hour time to 24-hour time 
$time_in_24_hour_format  = date("H:i", strtotime("1:30 PM"));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsugeshView Question on Stackoverflow
Solution 1 - PhpSudhir BastakotiView Answer on Stackoverflow