Standard for storing date/time?

When storing date time on a database (IE: post times, registered times, last login etc) what is the standard procedure for doing it so it translates through all time zones? For example, if i post something at 5pm and someone one time zone behind me views it, it will say 4pm for them… and so on. Whenever i enter something into a time stamp using the PHP date() function, it stores it as GMT and when i return it, it displays as the GMT time (6 hours ahead of me). So when i post at 2pm, it will display on the page as 8pm.

You can store it as any time, but a standard time zone as GMT is probably preferred as adjustments afterwards will make sense.

Just fetch the GMT date and adjust according to the current users settings.

I am storing it as GMT. But my question was how do i go about converting it depending on the current user? If i post an item at 2pm local time… it stores it on the server as 8pm (which is the GMT time). Now when i call that time to display on the page, it shows it as 8pm… which clearly is NOT the local time it was posted… So as asked in the original post, what is the standard for converting that to the viewers local time zone?

Just set default timezone to the users time

[php]date_default_timezone_set($user->timezone);[/php]

You can register the users timezone on register / have it in a user settings panel. Or you can try to detect it based on ip/location, browser settings (js) etc.

Ya after a bit of brain storming i came up with an idea to check their timezone with JS and put it into a hidden field on the login page then store it as a session. Thanks

Reneging on that response. I’m not sure if this is a bug with php… but gmdate is working in the opposite of what it’s supposed to… On login its storing my session as America/Chicago as it’s supposed to, then on loading the index after login its running date_default_timezone_set($_SESSION[“timezone”]);… then when i do a gmdate() it ADDS six hours rather than subtracting 6 hours like its supposed to… America/Chicago is 6 hours behind GMT… Anyone found a workaround for this bug or have a different method they prefer?

Nevermind, figured it out myself. Using gmdate to store the data, then returning it with just date + the offset (ie: -66060). Guess i just wasn’t fully aware of the minor differences between date and gmdate. Thanks

Sponsor our Newsletter | Privacy Policy | Terms of Service