[英]How do I convert 12 hour clock time into 24 hour clock time in PHP?
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?我究竟做错了什么?
From the docs for date()
: The H
format character gives the hour in 24h format.来自
date()
的文档: H
格式字符以 24 小时格式给出小时。 Also, you can use G
if you do not want the leading 0
for hours before noon.此外,如果您不想在中午之前的几个小时前导
0
,您可以使用G
Examples (if current time was seven-something-AM)示例(如果当前时间是上午七点多钟)
date('H:i:s')
-> "07:22:13" date('H:i:s')
-> "07:22:13"
date('G:i:s')
-> "7:22:13" date('G:i:s')
-> "7:22:13"
For your specific case:对于您的具体情况:
$timestamp = date("d/m/Y H:i:s", time());
根据手册,不同之处在于小时部分的大小写:“h”返回带前导零的 12 小时格式,01 到 12。“H”返回带前导零的小时的 24 小时格式, 01 至 23。
According to the manual , G
or H
give you the time in 24-hour format.根据手册,
G
或H
为您提供 24 小时格式的时间。 You should read the manual.你应该阅读手册。
date('H', time());
The H format character gives the hour in 24h format. H 格式字符以 24 小时格式给出小时。 Also, you can use g if you do not want the leading 0 for hours before noon.
此外,如果您不想在中午之前的几个小时内使用前导 0,则可以使用 g。
// 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"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.