Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How insert only year in mysql date column

Tags:

date

php

mysql

I have a date that sometimes can be only a year, years and month or a full date. What is the best practice to save it on the db?

I need to do search later as: Give me all date between 2004 - 2008 or give me all date between 2004-05-21 to 2005-12-31

like image 477
Atomico Avatar asked Sep 20 '25 05:09

Atomico


1 Answers

I would suggest to use DATE format (https://dev.mysql.com/doc/refman/5.5/en/datetime.html) and then if needed use more specific functions to query records by date ranges like

WHERE YEAR(created) BETWEEN 2010 AND 2011

etc.

More date\time related functions here: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

like image 104
Jinksy Avatar answered Sep 22 '25 22:09

Jinksy