简体   繁体   中英

php date can't get 24 digit time hour

I'm using date function $now=date("Ymd H:i:s"); in php 5.6.30, The browser output is string(19) "2017-04-21 02:54:54" ,That's abnormal.

php.ini set is date.timezone = PRC

Centos 7 system time:

[root@localhost sync]# date Fri Apr 21 14:53:20 CST 2017

While I was installed the PHP 7.0.16 in the same system, php.ini has the same config, but the date output normal in date function(is 24 digit time). Why happen this and how to let the date normal working.

This is a timezone issue.

In your php.ini, you have the timezone PRC (China) set, but your system time output gives the time in CST (Amerika).
In other words: Your PHP code does actually give you the 24-hour format, but in a different timezone, where it is in fact 02:54:54

If you need to get the time in a different timezone (like UTC ), you can set it like that:

date_default_timezone_set('UTC');

If you need a different local timezone, you can read about the possible values in the list of supported timezones in the PHP documentation

try this below code for date timezone

<?php
date_default_timezone_set('Asia/Shanghai');
echo date("Y-m-d H:i:s");
?>

I'm add the code to php.ini date.timezone = Asia/Shanghai , and install chrony and start it to synchronized. It works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM