Calculate age in years, months and days in mysql

By | July 19, 2017

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 |
+-----------------+------------+-----------------------------+

Like it? Share it