Page 1 of 1

Calculating age in number of years and number of months

Posted: Mon Jun 29, 2009 1:04 am
by sheikhmdirfan
Hi,


I would like to know as to how i can calculate age of a person years and months.
e.g say a person's DOB is 25-02-2008.. then it should show his/her age as
1 year and 4 months taking into consideration the number of days of each month..

Since all months dont have equal number of days..

How can i calculate his age in terms of years and number of months..


Any help is highly appreciated..

Thanks in advance..

Re: Calculating age in number of years and number of months

Posted: Mon Jun 29, 2009 2:21 am
by iamngk
you have to convert date into time stamp. that will make easy of your process.

Re: Calculating age in number of years and number of months

Posted: Mon Jun 29, 2009 3:42 am
by requinix
If you care about days then using timestamps is not the way to go.

Just do the math manually:
Year:
- Current year - birth year
- If the current month < the birth month, subtract one
- If the current month = the birth month and the current day < birth day, subtract one
Month:
- Current month - birth month
- If the current day < the birth day, subtract one
- If the number is negative, add 12
Day:
- Current day - birth day
- If the number is negative, add the amount of days in the last month

If today is June 29 2009,

Code: Select all

January 1 1990:   years=2009-1990=19,   months=6-1=5,       days=29-1=28     (19y  5m 28d)
June 28 1989:     years=2009-1989=20,   months=6-6=0,       days=29-28=1     (20y  0m  1d)
June 29 1988:     years=2009-1988=21,   months=6-6=0,       days=29-29=0     (21y  0m  0d)
June 30 1987:     years=2009-1987-1=21, months=6-6-1+12=11, days=29-30+31=30 (21y 11m 30d)
December 31 1986: years=2009-1986-1=22, months=6-12-1+12=5, days=29-31+31=29 (22y  5m 29d)
date is your friend.

Re: Calculating age in number of years and number of months

Posted: Mon Jun 29, 2009 4:19 am
by sheikhmdirfan
Thanks Sir...

I had done the same myself already...

Still Thanks a lot for ur help...

Re: Calculating age in number of years and number of months

Posted: Fri Jul 03, 2009 3:21 am
by DAVID87
This process seems like the way to go for me but can anyone help me with the code? I cant seem to get it working. I am probably doing something wrong.

I am wanting to follow the steps above.

I have users Dates of Birth stored as a date in a table and from that I need to have it show on my page as their current age. therefore if the date is the 22nd October 2009 and the DOB is 21st October 1999 they would be shown as 10, if they had a DOB of 22nd October 1999 they would be shown as 10 however if they had a DOB of 23rd October 1999 they would still be shown as 9. the current date needs to be the date that you are viewing the information (i.e. now)

Can anyone help?

Re: Calculating age in number of years and number of months

Posted: Fri Jul 03, 2009 8:17 am
by Eric!
Take a look at the code I posted here

viewtopic.php?f=1&t=102259