Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP date returning wrong time

Tags:

date

php

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.

like image 988
gargantuan Avatar asked Apr 13 '12 11:04

gargantuan


2 Answers

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

like image 102
Hassan Saeed Avatar answered Oct 19 '22 02:10

Hassan Saeed


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.

like image 43
phihag Avatar answered Oct 19 '22 03:10

phihag