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

前端 未结 7 2262
旧时难觅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:46
    $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));
    
    0 讨论(0)
提交回复
热议问题