Note that there are some explanatory texts on larger screens.

plurals
  1. POStoring dates and times in UTC and converting them local time in PHP/MySQL
    primarykey
    data
    text
    <p>I wanted to ask if this was going to work, before I went on a full fledged refactor of how i handle dates and times.</p> <p>To frame my question, here's my problem... I store "actions" taken by the user in a history table. These actions have a timestamp on them. Users can be from all over the world, and hence have different timezones. I want to display the items in the history and show the timestamps in one's local timezone.</p> <p>Also, I know I can use datetime fields and simply store UTC time formats, but I'm opting to use timestamp so i can set a default to current time.</p> <p>Will this work for my problem?</p> <ol> <li><p>My server time zone will be Los Angeles</p></li> <li><p>Store timestamps as a timestamp in MySQL. My understanding is that timestamp is always stored as UTC. Records will be stored by default as creation_date OR i will explicitly set UTC_TIMESTAMP(). Effictively, all records in the db will be UTC.</p></li> <li><p>When a user logs in I will grab their timezone (I store this in the db as a user setting). For this example, lets say this person is in New York.</p></li> </ol> <p>Here's where my question is. Will either of these work?</p> <ol start="4"> <li>a) Grab the history data. All timestamps will automatically be converted from UTC to Los Angeles time. Foreach history item, convert the Los Angeles timestamp to New York timestamp and echo.</li> </ol> <p>OR </p> <ol start="4"> <li>b) Set the php timezone "date_default_timezone_set" to New York. Grab the history data. All timestamps are STILL in Los Angeles time by server is still Los Angeles time. Foreach history item, just echo the timestamp because the "date_default_timezone_set" will automatically convert the Los Angeles timestamp to New York.</li> </ol> <p>Will option 4b work? Is there a better way to convert from the Los Angeles time to the New York time?</p> <p>Is there a better way to do this in general? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload