php and mysql date time difference

前端 未结 4 722
轮回少年
轮回少年 2021-01-24 15:20

I did this in php to store the lastlogin in a mysql datetime column

date(\'Y-m-d h:i:s\') //which then gave > \'2014-01-04 08:00:56\'

and th

相关标签:
4条回答
  • 2021-01-24 15:42

    I don't think that this would be a timezone issue because PHP and MySQL both are running on same machine so both would be getting same time on calling current datetime. May be you can try this because I tried this on my machine as you said.

    SELECT * FROM `elc_users` WHERE lastlogin < date_add(NOW(), INTERVAL -20 MINUTE) 
    
    0 讨论(0)
  • 2021-01-24 15:43

    Like Filip said it's probably a timezone issue; you can change the timezone of your script to whatever timezone your MySQL is set to like this:

    date_default_timezone_set('America/Los_Angeles');
    
    0 讨论(0)
  • 2021-01-24 15:44

    In order to avoid such problems, check the following things.

    1. Make sure that system time and system timezone of both the DB Server and the Application Server are same.
    2. Make sure that you have set the configuration of PHP and MySQL to take proper time-zones.
    3. Always use a single function to set update, insert rows that have a dateTime field in them. Personally, I suggest that you write a common function somewhere in your code, and whenever you need to get the current time, use that function, instead of using the MySQL's NOW()
    0 讨论(0)
  • 2021-01-24 15:45

    I solved the problem by deleting the date column and created it again. this now recorded the time in the correct format

    timestamp gave 2014-01-04 20:58:56, and NOW() gave 2014-01-04 20:58:56

    thanks for all who contributed, this wasn't a timezone issue as @AbdulJabbarWebBestow mentioned

    Thanks again

    0 讨论(0)
提交回复
热议问题