I want to store the date and time that a user performs an action on my website into a MySQL database. I’d like to be able to do the following with ease:
- Store the date and time as one field in the database
- Use a built in PHP or MySQL function to generate the date-time of the action
- Store the date-time based on my server’s time, and not worry about user timezones.
Order Bythe date-time field when I query MySQL- Later, display the date-time in many different formats using built in PHP methods
Here are my questions:
- What data type should I use in MySQL ( eg.
timestamp,datetime… )? - What method should I use to generate the date-time ( eg. MySQL’s
now(), PHP’sdate()… )? - What PHP method should I later use to format the date-time in various pretty ways ( eg. 23/4/2012, 5pm on Monday, July 2012 … )?
I would store it as a datetime, not a timestamp.
I normally use the PHP date function and that way if you ever want to store the time relative to the user’s timezone you can simply change the timezone based off the user’s settings.
When you pull it out of the database, use strtotime() to convert it, then you can use all the date() features to display it however you want. Example: