Anonymous Anonymous - 10 days ago 8
MySQL Question

Calculate difference between two dates in mysql to show years and months

How can I calculate difference between two dates ( current date and a date stored in database ) in mysql query to give result in years and months.

Suppose there are two dates -

2014-01-01
and
2015-12-28
then I want the result to be displayed as
1.11 years
. how can I do that ?

Right now I'm using
round(datediff(curdate(),e.joined_date)/365
which gives me value in years.

Please help.

Answer

use TIMESTAMPDIFF like this

SELECT CONCAT(TIMESTAMPDIFF(YEAR, '2014-06-01', '2015-08-01'), '.', TIMESTAMPDIFF(MONTH, '2014-06-01', '2015-08-01')-(TIMESTAMPDIFF(YEAR, '2014-06-01', '2015-08-01') * 12)) AS diff