Date and time in 24 hours format

强颜欢笑 提交于 2020-01-24 02:19:07

问题


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.

What I tried is:

$test = 'Fri, 15 Jan 2016 15:14:10 +0800';
$t = date('Y-m-d G:i:s',strtotime($test));
echo $t;

But it is displaying date in this format: 2016-01-15 7:14:10, it should be 2016-01-15 15:14:10.

How can i do this?


回答1:


Use H instead:

$test = 'Fri, 15 Jan 2016 15:14:10 +0800';
$t = date('Y-m-d H:i:s',strtotime($test));
echo $t;

H: 24-hour format of an hour with leading zeros 00 through 23

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())?

EDIT

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.

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");


来源:https://stackoverflow.com/questions/35127109/date-and-time-in-24-hours-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!