Calculating Age from Date of Birth in MySQL

I was doing some experiments the other day with MySQL and wrote some interesting queries to calculate age using MySQL. Here you go.

SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),”1978-03-28″)), ‘%Y’)+0 AS age

1:- DATEDIFF(NOW(),”1978-03-28″)
      This function
DATEDIFF() returns difference of two dates in days, e.g. DATEDIFF(“1978-04-28”, “1978-03-28”) will return 31 days. So by using NOW() i.e. current date, in the above query, we get, say, 10744 days.

2:- FROM_DAYS(10744)
      retuns the date starting from 0000-00-00 i.e. since year 0… so this function outputs “
0029-06-01″, i.e. the difference between two dates “1978-03-28” & “2008-08-27” is precisely 29 years, 6 months & 1 day. We need just years, so we use

3:-
DATE_FORMAT(“0029-06-01”, %Y) +0
      and it returns us 29 as an integer value.


SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(“1978-03-28”)), ‘%Y’)+0 AS age

1:- TO_DAYS(“1978-03-28”)
      The only new function in this query is
TO_DAYS(date), which converts our date to number of days starting from year 0 i.e. the opposite of function FROM_DAYS(days). So this gives us 722536 days. The other function TO_DAYS(NOW()) returns us 733280 days (for 2007-08-27). Subtracting the two, we get 733280 – 722536 = 10744 days. From here on, we move on to step 2 in the above scenario.


SELECT EXTRACT(YEAR FROM (FROM_DAYS(DATEDIFF(NOW(),”1978-03-28″))))+0 AS age

1:- EXTRACT(YEAR FROM “0029-06-01”) +0
     Taking the queue from step 2 in the first scenario, FROM_DAYS(10744) we apply another function. EXTRACT(date) which extracts a part from the given date as per the format, here YEAR, and it returns us 29 as an integer value.

This last query using EXTRACT() method is a bit slower than the one using DATE_FORMAT() – (.0001 sec). All these queries return the age in years (as a numeric value).

Powered by ScribeFire.

30 thoughts on “Calculating Age from Date of Birth in MySQL

  1. Thank you very much, this helped me alot. From this I’m sure that I’m able to choose which way is more efficient for calculating age using MySQL. Go ahead, and God bless you!

  2. hi ,
    i use this query for calculating difference b/w year or month .
    but its not working properly.means if i want to calculate more than one year difference it work good.
    but if i try to compute the diff of month within a year, its give null or zero values.
    because i need requirement like that.
    if u hv solution plz tell me

    Thank You

  3. I used this for calculate age
    SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(’2006-06-11′)-TO_DAYS(’2005-06-11′)), ‘%Y’)+0 AS age;

    If I want to make a condition “age BETWEEN 15 AND 25 ” any one know how to do?

    Thanks for help.

  4. grouping record by age (hope this will help kung):

    SELECT count(EXTRACT(YEAR FROM (FROM_DAYS(DATEDIFF(NOW(), m.dob))))+0) AS age_group, EXTRACT(YEAR FROM (FROM_DAYS(DATEDIFF(NOW(), m.dob))))+0 AS age from member m where m.dob!=’0000-00-00′ group by age

  5. Hi,
    try this query………..

    SELECT DATE_FORMAT(SUBDATE(CAST(FROM_DAYS(DATEDIFF(NOW(),SUBDATE(‘2009-05-05′,INTERVAL 1 YEAR))) AS DATE),INTERVAL 1 YEAR),’%y.%m’) AS AGE

    Hope it will help you……….
    Thanks

  6. I hv tried for date 01/01/1968 before date 01/01/1970 and its working fine.
    I hv tested this for MySql 4.1.

  7. beautiful.. i’ve used this to extract age from expression engine “date of birth” field_id_8 in DropDate format and it work like a sharm..
    SELECT EXTRACT( YEAR FROM ( FROM_DAYS( DATEDIFF( NOW() , field_id_8) ) ) ) +0 AS age FROM exp_weblog_data WHERE weblog_id = 9
    this is the query.
    now i can extract as i need record that have 40, between 40/50 years.
    thankyou!

  8. Try this
    TIMESTAMPDIFF(YEAR, dob, now())

    Where dob is your date of birth in db in mm/dd/yyyy format. This is accurate to the second using unix timestamp. The only flaw is that unix timestamp is based on paris’s timezone so you would have to keep your current timezone in relation with paris’s timezone to be %100 accurate, this aslo accounts for leap years.

  9. I realise this is a good few years later but I’m not sure these approaches are accurate.

    They’re delightfully neat but in taking things back to the years Of Our Lord I think this will fall foul of a change in the number of leap years.
    That might sound like a small niggle but it means that you only need to calculate about 500 people’s age before it becomes quite likely that one of them is wrong.

    To illustrate: The wisdom and maturity of the Son of Man may be partly due to his being God incarnate but maybe also that he was forced to wait 366 days to reach the ripe age of 1. By the age of 29 and 6 months he had lived through 7 more leap years.

    Our less holy example born in The Year of Bad Disco has gotten away with slightly quicker and cheaper birthday party thrill. 1978 was also a leap year but he was born in March so he just about managed to blag not having to wait the extra day for his first cake-on-fire-waved-in-child’s-face day.
    He has been through one less leap year than Jesus so whilst 10744 days have taken the Lord to 1st June, Discoboy has got to 2nd June.

    Now in this case, he’s 29 either way but if today were 28 March I think this approach would think he were still 28.

    I may have misunderstood and have told you a very pointless Messiah and Cake story but I don’t think so.

    The only way I can think of doing this accurately is a bit more clumsy and illustrated here (interestingly enough making the same mistake before correcting it)

    http://ma.tt/2003/12/calculate-age-in-mysql/

    Adam

  10. And this is not to mention how much havoc would be caused if systems were a little less heathen and took day 0 to be 25 December -1BC like the Lord EXEC cmded them to.

  11. Hey esto es un gran poste. Puedo utilizar una porcin en ella en mi sitio? Por supuesto ligara a su sitio as que la gente podra leer el artculo completo si ella quiso a. Agradece cualquier manera. edefkegfccgb

  12. Hello
    My dob in table is in timestamps format inserted by using time() function in php like value is 537195600.
    Now I want to calculate the current age with condition that fetch the records whose age is between 20 to 30. so how I can get these records.

    I have try all above but not get succeeded.

    Thanks

  13. Why not use the function that already exists for this purpose:
    SELECT TIMESTAMPDIFF(YEAR,’2002-05-01′,current_date) AS age;

  14. hello
    how if i want to calculate user’s birthdays days before and 7 days after?
    i have try all, its just calculate on the same year…

Leave a comment