gargantuan
gargantuan

Reputation: 8944

PHP date returning wrong time

The following script is returning the wrong time after I call date_default_timezone_set("UTC")

<?PHP   
    $timestamp = time();
    echo "<p>Timestamp: $timestamp</p>";

    // This returns the correct time
    echo "<p>". date("Y-m-d H:i:s", $timestamp) ."</p>";


    echo "<p>Now I call 'date_default_timezone_set(\"UTC\")' and echo out the same timestamp.</p>";
    echo "Set timezone = " . date_default_timezone_set("UTC");

    // This returns a time 5 hours in the past
    echo "<p>". date("Y-m-d H:i:s", $timestamp) ."</p>";

?>

The timezone on the server is BST. So what should happen is that the second call to 'date' should return a time 1 hour behind the first call. It's actually returning a time 5 hours behind the first one.

I should note that the server was originally set up with the EDT timezone (UTC -4). That was changed to BST (UTC +1) and the server was restarted.

I can't figure out if this is a PHP problem or a problem with the server.

Upvotes: 11

Views: 26696

Answers (3)

Hassan Saeed
Hassan Saeed

Reputation: 7130

just set your region with following code

   date_default_timezone_set("Asia/Bangkok");//set you countary name from below timezone list
    echo $date = date("Y-m-d H:i:s", time());//now it will show "Asia/Bangkok" or your date time

List of Supported Timezones http://www.php.net/manual/en/timezones.php

Upvotes: 5

Shycoder
Shycoder

Reputation: 21

You should check the php manual for the correct timezone of your country. Then set it in the date_default_timezone_set () function. For clarification, I explained it here http://t2techblog.com/php-a-simple-function-for-getting-the-current-nigeria-local-time/

Upvotes: 2

phihag
phihag

Reputation: 288260

This is almost certainly not an error in php, but in your local timezone configuration.

Most likely, you didn't actually change the system-wide time zone, but only that of an interactive display. Check that /etc/localtime matches what you'd expect. On debian systems, you can run tzselect (with superuser privileges) to set the system-wide timezone.

After setting the timezone, you may have to reset your clock. On many systems, that should happen automatically over time, but you can do it manually by running ntpdate -u pool.ntp.org.

Upvotes: 8

Related Questions