Is there any way to get the # of months and years since a date, using the mysql DATE column?
So if I enter 1/1/2009.. I want 1 years, 3 months.. and so on.
Is there any way to get the # of months and years since a date, using the mysql DATE column?
So if I enter 1/1/2009.. I want 1 years, 3 months.. and so on.
DateDiff can return the difference in days between two dates. From there, you might have to do the math yourself. You could make a Stored Procedure with all the logic, but for the most part SQL isn't designed as a functional language.
EDIT: I found what you are looking for. Try this:
DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(), <date>)), '%c months and %Y years')
select mnth as months_between, mnth/12 as years_between
from
( select (year(<current date column>) - year(<older date column>))*12 +
(month(<current date column>) - month(<older date column)) as mnth
from <tables with date columns>
where <conditions to get your rows with the dates you need to compare>
)
Also cf. Anthony Mollinaro's SQL Cookbook, chapter 8 on Date Arithmetic.