in PHP, Snippets, Tutorials

PHP: Convert 24 Hour Time to 12 Hour Time (and Vice Versa)

I just came across this in a time of need and thought I would share.

If you want to convert a time in 24 hour format, such as 14:00, to 12 hour format, which would be 2PM, you can use the following code.

24 Hour to 12 Hour:

Additionally, if you wanted to convert the 12 hour time format to 24 hour, you can use the next bit of code.

12 Hour to 24 Hour:

PS. Sorry I haven’t posted in a long long time. Been travelling, living, etc. Coding again and working on some new stuff so will try to post more often and reply to questions. Cheers readers!

Write a Comment