Jump to content

Storing local time, not server time in table


bulrush

Recommended Posts

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.

 

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?

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.