简体   繁体   中英

php - date_default_timezone_set not working, Why?

date_default_timezone_set not working.

my code:

ini_set('display_errors', true);
error_reporting(E_ALL);

date_default_timezone_set("UTC");
echo date('Y-m-d H:i:s T') . "<br>";
echo date('Y-m-d H:i:s T', time()) . "<br>";
date_default_timezone_set("Asia/Shanghai");
echo date('Y-m-d H:i:s T') . "<br>";
echo date('Y-m-d H:i:s T', time()) . "<br>";
ini_set("date.timezone","UTC");
echo date('Y-m-d H:i:s T') . "<br>";
echo date('Y-m-d H:i:s T', time()) . "<br>";
ini_set("date.timezone","Asia/Shanghai");
echo date('Y-m-d H:i:s T') . "<br>";
echo date('Y-m-d H:i:s T', time()) . "<br>";

all of them return the same date "2017-05-26 12:47:08 CST", why?


update:

I have fixed this problem, the reason is that I used the wrong way to change the timezone on CentOS7:

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

this way is right on CentOS6, but in CentOS7 /etc/localtime is linked to /usr/share/zoneinfo/Etc/UTC, so I damaged the UTC timezone.

the right way to change the timezone on CentOS7 is:

timedatectl set-timezone "Asia/Shanghai"

or

ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

so I copied /usr/share/zoneinfo/Etc/UTC from other system to my system to fixed this problem.

Try This.

<?php
    $now = new DateTime();
    $now->setTimezone(new DateTimeZone('America/Los_Angeles'));
    echo $now->format('Y-m-d H:i:s T');
?>

time() is timezone independant. This means, it will always return the time in seconds since january 1 1970, no matter how the timezone is configured. It always takes the UTC-time.'

date_default_timezone_set(); NOT working

Also Check this http://php.net/manual/pl/function.time.php#100220

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