如何在 PHP 中将 12 小时制时间转换为 24 小时制时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3092558/
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
How do I convert 12 hour clock time into 24 hour clock time in PHP?
提问by MySQL Rocks
I am using following function and I want time in 24hr clock format but this gives me time in 12hrs:
我正在使用以下功能,我想要 24 小时时钟格式的时间,但这给了我 12 小时的时间:
<?php
date_default_timezone_set('Asia/Kolkata');
$timestamp = date("d/m/Y h:i:s", time());
print $timestamp ;
?>
What am I doing wrong?
我究竟做错了什么?
回答by jensgram
From the docs for date(): The Hformat character gives the hour in 24h format. Also, you can use Gif you do not want the leading 0for hours before noon.
来自以下文档date():H格式字符以 24 小时格式给出小时。此外,G如果您不想0在中午之前的几个小时领先,您可以使用。
Examples(if current time was seven-something-AM)date('H:i:s')-> "07:22:13"date('G:i:s')-> "7:22:13"
示例(如果当前时间是上午date('H:i:s')七点多钟)->“07:22:13” date('G:i:s')->“7:22:13”
For your specific case:
对于您的具体情况:
$timestamp = date("d/m/Y H:i:s", time());
回答by Webdezyner
According to the manual the difference is in the capitalization of hour portion: "h" returns a 12-hour format of an hour with leading zeros, 01 through 12. "H" returns a 24-hour format of an hour with leading zeros, 01 through 23.
根据手册,不同之处在于小时部分的大小写:“h”返回带前导零的 12 小时格式,01 到 12。“H”返回带前导零的小时的 24 小时格式, 01 至 23。
回答by Johannes Gorset
According to the manual, Gor Hgive you the time in 24-hour format. You should read the manual.
根据手册,G或者H给你24小时制的时间。你应该阅读手册。
date('H', time());
date('H', time());

