0

I am developing a site locally, when a user registers on the website the current date and time is stored in a TIMESTAMP column, but if I do time() in my php script the time is 2 hours late compared to the timestamp column!

So for example I registered around 30mins ago, and the time in the database column is 08/31/2016 18:23:12 , and in my php script if I echo time() now I get 08/31/2016 16:51:22 (when turned into a readable date format). I was expecting to get 08/31/2016 18:51:22

Why do the times differ since it's the same local server? I don't care about timezones for this particular problem, I just want the mysql timestamp and php time() to be the same "zone".

user1091508
  • 61
  • 2
  • 8
  • Are you using a shared container (Like Godaddy?) Because the MYSQL might be stored in the Server default time and your php might be 'adjusted' to your location – Forbs Aug 31 '16 at 16:59
  • No, I am using wamp on my computer. – user1091508 Aug 31 '16 at 17:00
  • I think you'll have to care about timezones for this problem. – Don't Panic Aug 31 '16 at 17:03
  • It looks like your PHP and MySQL server are set to different timezones because both the now() and the time() function return the current time dependent on the timezone. – pBuch Aug 31 '16 at 17:04
  • 1
    Answer is in here http://stackoverflow.com/questions/8596083/apache-time-stamp-incorrect – Forbs Aug 31 '16 at 17:04

1 Answers1

0

This is generally cause you need to set the timezone for your PHP script.

Here's an example:

<?php

// This is some Asian timezone, so your time() function will match this timezone
date_default_timezone_set("Asia/Bangkok");

?>

My suggestion is to use <?php $timestamp = time(); ?> and just use MySQL Insert and insert that into your db instead of using MySQL timestamp type.

Use this function to set your timezone, you can view all the timezones at http://www.php.net/manual/en/timezones.php

hope this helps!!!

Community
  • 1
  • 1