56

For example, I have a time in this format:

eg. 

09:15 AM
04:25 PM
11:25 AM

How do I convert it to :

09:15
16:25
23:25

Currently my code is :

$format_time = str_replace(" AM", "", $time, $count);
if ($count === 0){
    $format_time = strstr($time, ' PM', true);
    $format_time = ......
}

However, it seems there are some easier and more elegant way to do this?

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

How do I fit the above sample in my case? Thanks.

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
user1871516
  • 979
  • 6
  • 14
  • 26

9 Answers9

139

Try with this

echo date("G:i", strtotime($time));

or you can try like this also

echo date("H:i", strtotime("04:25 PM"));
GautamD31
  • 28,552
  • 10
  • 64
  • 85
  • 3
    `G` - 24-hour format of an hour without leading zeros - 0 through 23; `H` - 24-hour format of an hour with leading zeros - 00 through 23 – Matthew Sep 24 '20 at 06:41
11

If you use a Datetime format see http://php.net/manual/en/datetime.format.php

You can do this :

$date = new \DateTime();
echo date_format($date, 'Y-m-d H:i:s');
#output: 2012-03-24 17:45:12

echo date_format($date, 'G:ia');
#output: 05:45pm
Babou34090
  • 381
  • 3
  • 8
9

PHP 5.3+ solution.

$new_time = DateTime::createFromFormat('h:i A', '01:00 PM');
$time_24 = $new_time->format('H:i:s');

Output: 13:00:00

Works great when formatting of date is required. Check This Answer for details.

Abdul Rehman
  • 1,662
  • 3
  • 22
  • 36
8

You can use this for 24 hour to 12 hour:

echo date("h:i", strtotime($time));

And for vice versa:

echo date("H:i", strtotime($time));
cakan
  • 2,099
  • 5
  • 32
  • 42
4

We can use Carbon

 $time = '09:15 PM';
 $s=Carbon::parse($time);
 echo $military_time =$s->format('G:i');

http://carbon.nesbot.com/docs/

sumit
  • 15,003
  • 12
  • 69
  • 110
2
$Hour1 = "09:00 am";
$Hour =  date("H:i", strtotime($Hour1));  
Viral M
  • 261
  • 2
  • 14
1
$s = '07:05:45PM';
$tarr = explode(':', $s);
if(strpos( $s, 'AM') === false && $tarr[0] !== '12'){
    $tarr[0] = $tarr[0] + 12;
}elseif(strpos( $s, 'PM') === false && $tarr[0] == '12'){
    $tarr[0] = '00';
}
echo preg_replace("/[^0-9 :]/", '', implode(':', $tarr));
  • I would suggest you to add additional details on what your approach is solving and how. Dumping lines of code without any additional information can become more confusing than helpful if the person asking the question doesn't fully understand the way the problem was solved. It's always a good idea to add a brief description – Hugo Noro Feb 06 '21 at 18:43
  • You can use this code for convert 12 hour time to 24 hour time. – Saurabh Mani Pandey Feb 07 '21 at 19:26
0
$time = '09:15 AM';

$chunks = explode(':', $time);
if (strpos( $time, 'AM') === false && $chunks[0] !== '12') {
    $chunks[0] = $chunks[0] + 12;
} else if (strpos( $time, 'PM') === false && $chunks[0] == '12') {
    $chunks[0] = '00';
}

echo preg_replace('/\s[A-Z]+/s', '', implode(':', $chunks));
Michele Carino
  • 1,043
  • 2
  • 11
  • 25
-1

We Can Create AM/PM by Carbon Laravel

Carbon::parse('your Value')->format('g:i A');
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 16 '23 at 17:01