PDA

View Full Version : Calculate age and produce output like "Yr.Mth"


raffiq_eddy
05-18-2009, 05:13 AM
Hi,

Using date I want to produce output like this:

7.8 OR
7 Years 8 Months

I found solution, as following:

DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(Employee.dDOEmp)), '%y.%m')

the result look like so:

01.05 (or 1 year 5 months)

But the problem is, when the result is below 1 year, it showed as following:

00.00

Can anyone help me to solve this one?

TIA

oesxyl
05-18-2009, 10:03 AM
try to use period_diff:

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_period-diff

you must convert both dates to YYMM or YYYYMM format first:

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html

the result will be in months and is easy to convert to years + month after that.

best regards

raffiq_eddy
05-19-2009, 03:51 AM
OK, I'll try now

Thanks oesxyl!