Can someone please clarify how MySQL's TIMESTAMP is used in conjunction with PHP's DateTime class?
683
I've been studying the differences in usage between MySQL's DATETIME and TIMESTAMP. It seems that it's pretty straight forward with DATETIME. I would use the following procedure:
- Choose the default timezone for all dates, such as UTC.
- Let user select a date from drop-down.
- Create new PHP DateTime object with the chosen date, using the user's timezone settings, such as EST.
- Convert the object to UTC, and insert into database.
- On another page, retrieve datetime value and make a new DateTime object with it, using UTC timezone.
- Convert object to user's local time (EST), and display to him.
Now, it seems that mysql's TIMESTAMP column type can help eliminate all of these conversions.
- What would the above steps look like with the TIMESTAMP column type?
- Would I need to do "SET time_zone = timezone;" in the beginning of each pageload to set the timezone to the location of the user?
- Would it be easier to ONLY use one type of date column type per database? If not, it may require two different sets of functions to produce the right date.
- Should TIMESTAMP only be used in columns not intended to be shown to the public (so as not to deal with formatting)? Like when a row was created, last edited, etc.
Answer
Solution:
I have not tested any of this approach, but it seems pretty straightforward =)
Traditionally timestamp is associated like you mention, and datetime for other dates.
more on locale/time zone: MYSQL set timezone in PHP code