你如何在 PHP 中的 12 小时制和 24 小时制之间转换?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8742191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:23:51  来源:igfitidea点击:

How do you convert between 12 hour time and 24 hour time in PHP?

php

提问by sugesh

I have a time as a string, for example:

我有一个时间作为字符串,例如:

$time = '5:36 pm';

I require this to be in 24 hour format (i.e. 17:36), however I don't know which functions I need to use to convert it. Please help.

我要求这是 24 小时格式(即17:36),但是我不知道我需要使用哪些函数来转换它。请帮忙。

回答by Sudhir Bastakoti

// 24-hour time to 12-hour time 
$time_in_12_hour_format  = date("g:i a", strtotime("13:30"));

// 12-hour time to 24-hour time 
$time_in_24_hour_format  = date("H:i", strtotime("1:30 PM"));