30

I have to store in my mysql database, the users's birthday ( like 12-11-1990 ).

What field type do I have to use to store it ?

5
  • 4
    do you mean what field type date
    – mgraph
    Commented Apr 30, 2012 at 19:41
  • 2
    personally i love timestamps, but you should probably go with oezi's answer, MYSQL DATE are the official way to go. Commented Apr 30, 2012 at 19:42
  • Make sure you store the Locale. See my answer.
    – Adam Gent
    Commented Apr 30, 2012 at 19:52
  • You're asking about storing a birthday with the year. If you want to store it without the year (which is how I interpreted your question before looking at the answers, since otherwise I don't know why you're asking about birthdays specifically and not just dates in general), then just store as an integer between 1 and 366, but remember that when converting back to a date, you need to subtract 1 beginning on March 1st unless it's a leap year. Commented Feb 6, 2014 at 21:44
  • 2
    TIMESTAMP is not suitable for storing a date of birth. The range of supported values for TIMESTAMP is '1970-01-01 00:00:01.000000' to '2038-01-19 03:14:07.999999'. I generally suggest avoiding TIMESTAMP unless you have a good reason to choose it over DATE, TIME or DATETIME.
    – Mavelo
    Commented Aug 18, 2017 at 22:22

5 Answers 5

32

to store a date, you should probably use a date. to quote the documentation:

The DATE type is used for values with a date part but no time part.

14

You can store it as a DATE object as you would normally do with non-repeating dates. The, if you're using MySQL (I believe other DBMS also have functions for this) you can retrieve birthdays using MySQL functions as described here:

SELECT * 
FROM table_name 
WHERE 
    MONTH(date_field) = desired_month AND 
    DAY(date_field) = desired_day

This method can be a bit slow when dealing with thousands of records. If that's your case, you can store the birthday with 3 separate INTs, one for each (year, month, day). Then you search birthdays like this:

SELECT *
FROM table_name
WHERE
   month_field = desired_month AND
   day_field = desired_day

If you use this last method, I'd advise you to also store a DATETIME version of the birthday so that you can build the date without any kind of maneuvers. You can additionally index each one of the 3 fields (non-unique) so it's faster to retrieve them.

1
  • 2
    Include Feb. 29th birthdays on Mar. 1st for non-leap years. Commented Apr 30, 2012 at 20:18
6

"DATE" should be good.

The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is '1000-01-01' to '9999-12-31'.

More details : http://dev.mysql.com/doc/refman/5.5/en/datetime.html

5

You should store the date as DATE.

And the Locale as a varchar (PHP or Java).

Don't forget the Locale :)

Nothing like getting an Email from something like Weibo (Chinese Twitter) when its not your birthday yet.

1

is DATE too obvious?

1
  • 3
    Curious how people interpreted this. I actually read it as "is it not DATE your looking for as it seems obvious?". That is @Jason may have had doubts about his answer. Consequently +1 for me to give him benefit of doubt.
    – Adam Gent
    Commented Apr 30, 2012 at 19:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.