How to convert the time from AM/PM to 24 hour format in PHP?

前端 未结 7 2261
旧时难觅i
旧时难觅i 2020-12-07 22:58

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


        
相关标签:
7条回答
  • 2020-12-07 23:19

    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));
    
    0 讨论(0)
  • 2020-12-07 23:23

    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
    
    0 讨论(0)
  • 2020-12-07 23:25

    We can use Carbon

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

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

    0 讨论(0)
  • 2020-12-07 23:30
    $Hour1 = "09:00 am";
    $Hour =  date("H:i", strtotime($Hour1));  
    
    0 讨论(0)
  • 2020-12-07 23:35

    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 date is required. Check This Answer for details.

    0 讨论(0)
  • 2020-12-07 23:44

    Try with this

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

    or you can try like this also

    echo date("H:i", strtotime("04:25 PM"));
    
    0 讨论(0)
提交回复
热议问题