gdtv
gdtv

Reputation: 121

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.

Upvotes: 3

Views: 9551

Answers (1)

Adharsh M
Adharsh M

Reputation: 2812

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

Upvotes: 9

Related Questions