Store users’ birth dates, not ages, in the database

If you store your users’ ages in the database, in one year’s time their ages will be wrong. If you store your users’ birth dates in the database, and calculate their age from that, this age will always be correct.

Some sites wish to ask the user their age and display it. It would seem simplest to just store this number in an integer field alongside the user in the database. But you can be sure that, in one year, this value will be wrong.

Instead you should ask the user their birth date. Store that in the database. Always calculate their age by seeing how many years have passed between their stored birth date and the current date. As they get older, their age will always be displayed correctly.

In case you really wish to ask the user their age only, one trick we did at uboot, was to calculate an approximate birth date. Assuming they’re half way between their birthdays, and they say they are n years old, assume their birth date was n+½ years ago (or 12n+6 months ago). Calculate the age to display as described above. On the day they enter their age, the display will be correct. One year thence it’ll be correct as well. In between it’ll be, well, an approximation. But better than displaying their entered age forever—showing them never ageing.

3 Responses to “Store users’ birth dates, not ages, in the database”

  1. Robin Salih Says:

    Are their websites that actually store age not date of birth? What was the reason for not wanting their full date of birth on uboot? Was it privacy reasons?

  2. adrian Says:

    At uboot, I think it was considered better usability to allow the user to enter their age. After all, it was the age which was going to be displayed to other users. The person who decided that probably didn’t understand that ages can change over time.

    I have literally seen “age NUMBER” fields in databases twice in my professional career. In both cases I’ve corrected it. Next time I see it I will correct it AND link to a blog post describing the dangers of the approach.

  3. Herbert Rusznak Says:

    Another nice fault: Using the german word for “age” -> “alter” :D

    i’ve seen that several times ago, “newbies” asking why that fieldname does not work :)

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

For inserting HTML or XML please remember to use &lt; instead of <