Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try with this

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

or you can try like this also

echo date("H:i", strtotime("04:25 PM"));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...