bulrush Posted May 19, 2010 Share Posted May 19, 2010 In my PHP app, whenever a person logs in, I store the current date and time in the 'user' table in the 'lastlogin' column, which is a DATE type. Here is my SQL statement: $query = "UPDATE user SET numlogin=".$n.", ". "lastlogin=NOW() ". "WHERE userid=".$_SESSION['myuserid'].";"; I also increment the number of logins, just for fun. However the NOW() SQL function is storing the server time (I believe). How can I get it to store my local time? I want to add 3 hours to the time I'm getting from NOW(). Also, my American city does Daylight savings time, so the difference will sometimes be 3 hours, sometimes 4 hours. How can I account for that? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/202276-storing-local-time-not-server-time-in-table/ Share on other sites More sharing options...
bulrush Posted May 19, 2010 Author Share Posted May 19, 2010 Perhaps I can do this another way. I have my 'lastlogin' as an SQL 'datetime' type field. Is there a PHP function which returns the local time which can be stored in this field type? Or is there a way I can get the server time and just add 3 hours using PHP? Quote Link to comment https://forums.phpfreaks.com/topic/202276-storing-local-time-not-server-time-in-table/#findComment-1060706 Share on other sites More sharing options...
Mchl Posted May 19, 2010 Share Posted May 19, 2010 It is best to store a GMT time in database, and convert it to user timezone when displaying. See some useful functions here: http://www.php.net/manual/en/ref.datetime.php Quote Link to comment https://forums.phpfreaks.com/topic/202276-storing-local-time-not-server-time-in-table/#findComment-1060736 Share on other sites More sharing options...
bulrush Posted May 19, 2010 Author Share Posted May 19, 2010 I don't think my Mysql server is storing the GMT time, I think it is storing the time local to the server in California. I'm in Michigan. But I see what you are saying. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/202276-storing-local-time-not-server-time-in-table/#findComment-1060737 Share on other sites More sharing options...
ale8oneboy Posted May 19, 2010 Share Posted May 19, 2010 It is best to store a GMT time in database, and convert it to user timezone when displaying. See some useful functions here: http://www.php.net/manual/en/ref.datetime.php I agree. I've stored using the local time of the server and converted before. It's not fun when you forget to change server time zones later. Quote Link to comment https://forums.phpfreaks.com/topic/202276-storing-local-time-not-server-time-in-table/#findComment-1060739 Share on other sites More sharing options...
bulrush Posted May 19, 2010 Author Share Posted May 19, 2010 So GMT time does not have Daylight Savings Time like the US does in some areas? Quote Link to comment https://forums.phpfreaks.com/topic/202276-storing-local-time-not-server-time-in-table/#findComment-1060741 Share on other sites More sharing options...
Mchl Posted May 19, 2010 Share Posted May 19, 2010 That's one of the reasons it's so useful. Quote Link to comment https://forums.phpfreaks.com/topic/202276-storing-local-time-not-server-time-in-table/#findComment-1060744 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.