I tried this but how to calculate for leap years.
SELECT
FLOOR(DATEDIFF(NOW(), date_of_birth) / 365.25) as years,
FLOOR(MOD(DATEDIFF(NOW(), date_of_birth), 365.25) / 30.4375) as months,
MOD(DATEDIFF(NOW(), date_of_birth), 30.4375) as days
FROM employees;
Can anyone please help
2
Answers
See https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_timestampdiff
Re your comment:
If MySQL date functions didn’t handle leap years, they’d be pretty useless, right? In fact, they do know how to handle leap years.
Thus there are 11 leap years between 1980 and today (I am writing this on 2023-01-30).
For example:
Of course you may use according expressions instead of UDVs.
But there is a problem. The number of days per month vary, so the days amount in years-months-days may differ depends on the calculation direction. My query adds previously calculated years and months to
date_of_birth
, you may create similar query which substracts years and months from current date.. and the days amount may differ.