Ian Y.
Ian Y.

Reputation: 2457

Changing the time zone in PHP is 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 '<br>';
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 '<br>';
$now = new DateTime();
$now->setTimezone(new DateTimeZone('UTC'));
echo $now->getTimestamp();

And the two echoed timestamps were still the same.

Upvotes: 2

Views: 760

Answers (3)

deceze
deceze

Reputation: 522016

getTimestamp returns a UNIX Epoch timestamp. UNIX timestamps are always the same all over the world. Time zones only affect human readable time, e.g. when you do $now->format('H:i:s').

Also see https://stackoverflow.com/a/4812178/476 for more explanation.

Upvotes: 4

VIKAS KATARIYA
VIKAS KATARIYA

Reputation: 6005

Try this

use date_default_timezone_set("UTC")

<?php
 date_default_timezone_set("UTC");
 echo date_default_timezone_get().date('Y-m-d h:I:s');
?>

ExampleDemo

Upvotes: 0

Amit Sharma
Amit Sharma

Reputation: 1795

Check this way if you able to set the time zone or not.

 date_default_timezone_set("UTC");
 echo date_default_timezone_get();
 echo date(“Y-m-d h:I:s”);

Hope this helps

Upvotes: -1

Related Questions