Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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.

like image 656
sugesh Avatar asked Jan 05 '12 12:01

sugesh


People also ask

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

Converting time from a 12-hour clock to a 24-hour clock To work out the 24-hour reading of an afternoon time (PM), you must add 12 to the hour shown on a 12-hour clock. For example, 1:00 PM (12-hour) would become 13:00 (24-hour).

How can change time in 24-hour format in PHP?

$time = '23:45'; echo date('g:i a', strtotime($time));

How do I convert 12 hours date to 24 hours datetime in SQL?

In SQL Server 2012, we can use Format function to have suitable date time format. Use capital letter 'HH:mm:ss' for 24 hour date time format.


1 Answers

// 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"));
like image 122
Sudhir Bastakoti Avatar answered Oct 17 '22 11:10

Sudhir Bastakoti