I have an input for date. The format is YYYY-MM-DD. However, MM and DD are optional. All the following constitute for expected format:
- 2012-10-13
- 2012-09
- 2010
I need to store this data in MySQL database. It is going to be used for both: to display date and to filter (query) results.
What is the way to do it?
Simply set the month/day to
00in aDATETIMEcolumn. For example:All of those examples are perfectly valid in
DATETIMEcolumns. As working proof, we (the company I work for) use this to specify book’s published dates for our website. We’re currently on MySQL 5.1.54.Note: Depending on the language used to format dates (if any), you may need to format them differently depending on whether day/month is provided. For example, in PHP, I needed to check if the day was provided, and manually change the last ‘-00’ to ‘-01’, and then format the date appropriately (leaving out the day in the final result).
From the Manual (source)
Thanks to @jon for tracking this down: