Home > Article > Backend Development > How to convert 12-hour clock to 24-hour clock in php_PHP tutorial
This article describes the method of converting 12-hour clock to 24-hour clock in php. Share it with everyone for your reference. The details are as follows:
php converts 12-hour clock to 24-hour clock. The input format is: 02:30:00 pm converted to: 14:30:00
4 11 |
<๐>function to_24_hour($hours,$minutes,$seconds,$meridiem){<๐>
<๐>$hours = sprintf(' d',(int) $hours);<๐>
<๐>$minutes = sprintf(' d',(int) $minutes);<๐>
<๐>$seconds = sprintf(' d',(int) $seconds);<๐>
<๐>$meridiem = (strtolower($meridiem)=='am') ? 'am' : 'pm';<๐>
<๐>return date('H:i:s', strtotime("{$hours}:{$minutes}:{$seconds}{$meridiem}"));<๐>
<๐>}<๐>
<๐>echo to_24_hour( 1, 2, 3, 'pm' ); // Returns 13:02:03<๐>
<๐>echo to_24_hour( '02', '30', '00', 'pm' ); // Returns 14:30:00<๐>
<๐>?>
|