Hướng dẫn php timezone not working

My hosting company set the default time zone in my php.ini to America/Chicago. I confirmed the time zone by checking phpinfo[] and echoing date_default_timezone_get[] in my PHP code.

However, I tested changing the time zone in my PHP code but had no luck.

Firstly, I tested:

echo [new DateTime[]]->getTimestamp[];
echo '
'; date_default_timezone_set['UTC']; echo [new DateTime[]]->getTimestamp[];

And the two echoed timestamps were the same.

Then, I tested:

echo [new DateTime[]]->getTimestamp[];
echo '
'; $now = new DateTime[]; $now->setTimezone[new DateTimeZone['UTC']]; echo $now->getTimestamp[];

And the two echoed timestamps were still the same.

I am trying to have a line of code added to an html document that is preceded by the time. I want the time zone to be relative to me, however I cannot change it from the default UTC. I have changed in the php.ini file to PST as well as using date_default_timezone_set['America/Los_Angeles']; and yet it still prints the time 7 hours ahead of my timezone. Heres the code that deals with the time:

session_start[];
if[isset[$_SESSION['name']]]
{
    date_default_timezone_set['America/Los_Angeles'];

    $msg = $_POST['text'];

    $fo = fopen["log.html", 'a'];
    fwrite[$fo, "
[".date["g:i A"]."] ".$_SESSION['name'].": ".stripslashes[htmlspecialchars[$msg]]."
"]; fclose[$fo]; }

asked Aug 11, 2012 at 4:14

Xander LucianoXander Luciano

3,6576 gold badges31 silver badges52 bronze badges

3

Servers should be set to UTC, and you should not be looking to change the default. Instead, what you want to do is create a DateTime object based on the time, then convert it to the timezone you want, and display.

$now = new DateTime[];
$now->setTimezone[new DateTimeZone['America/Los_Angeles']];
echo $now->format['g:i A'];

I don't know if your format string is valid or not, but the format method is suppossed to be compatible with that accepted by the date[] function you were using in your original example.

answered Aug 11, 2012 at 4:33

gviewgview

14.3k3 gold badges43 silver badges48 bronze badges

3

First make sure you're using a valued timezone. You can find a list of supported timezones in the PHP docs.

The second problem is using date[] without specifying the timestamp. This defaults to the timestamp produced by time[] which [based on a comment in the documentation] is UTC time. You'll either have to use strftime[] or manually subtract the difference from UTC.

answered Aug 11, 2012 at 4:17

BrianSBrianS

12.4k12 gold badges58 silver badges117 bronze badges

8

If you use 'etc/GMT' you can set the dateTime object to the desired time zone like so:

$dtz = new DateTimeZone['etc/GMT-10'];
$dt = new DateTime[date["Y-m-d h:i A"], $dtz]; 
$date = gmdate["Y-m-d h:i A", $dt->format['U']];

answered Aug 11, 2012 at 6:14

Lawrence DeSouzaLawrence DeSouza

9445 gold badges14 silver badges31 bronze badges

Not the answer you're looking for? Browse other questions tagged php date timezone or ask your own question.

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'] . "
"; echo date['Y-m-d H:i:s T', time[]] . "
"; date_default_timezone_set["Asia/Shanghai"]; echo date['Y-m-d H:i:s T'] . "
"; echo date['Y-m-d H:i:s T', time[]] . "
"; ini_set["date.timezone","UTC"]; echo date['Y-m-d H:i:s T'] . "
"; echo date['Y-m-d H:i:s T', time[]] . "
"; ini_set["date.timezone","Asia/Shanghai"]; echo date['Y-m-d H:i:s T'] . "
"; echo date['Y-m-d H:i:s T', time[]] . "
";

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.

Chủ Đề