timestamp in mysql and time() different locally

时间:2016-08-31 16:56:01

标签: php date timestamp

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".

1 个答案:

答案 0 :(得分: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!!!