24小时格式的日期和时间 [英] Date and time in 24 hours format
问题描述
我有一个格式为Fri, 15 Jan 2016 15:14:10 +0800
的日期,我想显示这样的时间2016-01-15 15:14:10
.
I have a date in this format Fri, 15 Jan 2016 15:14:10 +0800
, and I want to display time like this 2016-01-15 15:14:10
.
我尝试过的是:
$test = 'Fri, 15 Jan 2016 15:14:10 +0800';
$t = date('Y-m-d G:i:s',strtotime($test));
echo $t;
但是它以以下格式显示日期:2016-01-15 7:14:10
,应为2016-01-15 15:14:10
.
But it is displaying date in this format: 2016-01-15 7:14:10
, it should be 2016-01-15 15:14:10
.
我该怎么做?
推荐答案
使用 H
代替:
$test = 'Fri, 15 Jan 2016 15:14:10 +0800';
$t = date('Y-m-d H:i:s',strtotime($test));
echo $t;
H :一个小时的24小时制,其前导零为00到23
H: 24-hour format of an hour with leading zeros 00 through 23
G应该是相同的,但是没有前导零.我怀疑您的PHP设置为与+0800
不同的时区.您可以确认您的时区(date_default_timezone_get()
)吗?
G should be the same, but without leading zeroes though. I suspect that your PHP is set to a different timezone than +0800
. Can you confirm your timezone (date_default_timezone_get()
)?
OP确认他的时区设置为UTC,在这种情况下,由于date
使用PHP的默认时区,因此它掩盖了早上7点的完美含义.
OP confirmed that his timezone was set to UTC, in which case it maskes perfect sense that it shows 7 in the morning, as date
uses PHPs default timezone.
如果要继承"时区,同时获得更大的灵活性,则应切换到 DateTime
:
If you want to "inherit" the Timezone, while getting more flexibility, you should switch to DateTime
:
echo (new DateTime($test))->format("Y-m-d H:i:s");
这篇关于24小时格式的日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!