I've been reading up on this topic for the past few hours, and I think I've got a handle on it, but I'd like some confirmation.
Situation
I want a user in, say, California to be able to post a comment that will be stored in MySQL. I then want a user in, say, Texas to be able to view the comment with the post date adjusted to his or her time zone.
Proposed Solution
Storing
- Run the following at the start of the application so that all date functions use UTC timezone:
date_default_timezone_set('UTC');
$Date = new DateTime();
to get a DateTime object with the current date and time in UTC.- Use
$Date->format()
to get the value to insert into the datetime type column in MySQL.
Displaying
- Get the user's timezone information from JavaScript and store it in a cookie.
- Run a MySQL SELECT query to retrieve the datetime column value.
$Date = new DateTime($row['time']);
to instantiate a DateTime object with the stored UTC time.$Date->setTimezone(new DateTimeZone($userTimezone));
to adjust the UTC time to the user's timezone.- Display using
$Date->format();
Is that the gist of what has to be done? Am I missing a better solution? Thanks for your help!