Calculate age in years months and days in mysql
Posted by navaneeth on Mar 17, 2015 in General | 2 Comments

When we are dealing with dates in mysql sometimes we need to calculate the age in years months and days directly from the mysql query
We can easily achieve it from the below sql query
SELECT name,dob, CONCAT(TIMESTAMPDIFF( YEAR, dob, now() ),' Years,', TIMESTAMPDIFF( MONTH, dob, now() ) % 12,' Months,', FLOOR( TIMESTAMPDIFF( DAY, dob, now() ) % 30.4375 ),' Days') as AGE from users
And the output of the query will shown like this
+-----------------+------------+-----------------------------+ | name | dob | Age | +-----------------+------------+-----------------------------+ | Arun | 1989-09-07 | 25 Years, 6 Months, 8 Days | | Disilva | 1960-12-04 | 54 Years, 3 Months, 11 Days | | Pam | 1990-07-03 | 24 Years, 8 Months, 13 Days | +-----------------+------------+-----------------------------+
2 Comments on “Calculate age in years months and days in mysql”
manjiri
December 27, 2015the above query does not consider all the possible values of dob and now(_;
navaneeth
December 28, 2015Pls share the possible values