11

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.

5
  • What is date_default_timezone_get() showing before you change the timezone? Commented Apr 13, 2012 at 11:21
  • Time zone: Europe/London Commented Apr 13, 2012 at 11:22
  • And what is your server's date terminal command returns? Commented Apr 13, 2012 at 11:23
  • -bash-3.2$ date -> Fri Apr 13 12:08:21 BST 2012 Commented Apr 13, 2012 at 11:24
  • That time is completely wrong either way. Run ntpdate to correct your computer's clock (see my updated answer). Commented Apr 13, 2012 at 11:36

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Where to run ntpdate -u pool.ntp.org. ? In cpanel ?
@TarangP On a command line shell on the system you have, typically via ssh. If all you have is cpanel, see the cpanel documentation on how to access the command line. cpanel also has its own NTP/time interface, which can be used instead.
tzselect I used this on ubuntu, configured the timezone well and restarted apache2 but I still get the wrong date!
5

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

1 Comment

That one helped me showing current time once on shared hosting, onle the time() is not really needed... thanks!
2

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/

3 Comments

Perhaps you could provide an example.
Example: a simple function that returns the correct time in my country(Nigeria) with a welcome message. <?php function get_ng_time() { date_default_timezone_set('Africa/Lagos'); $time = date('h:i A'); $message = 'Hello, welcome to my blog. It is '.$time.' here in Nigeria.'; return $message; } ?>
I'm really sorry that the function and my texts are kind of jam-packed together. I'm new here.. Still figuring out how things work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.