Calculating age in number of years and number of months

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sheikhmdirfan
Forum Newbie
Posts: 22
Joined: Tue Jan 20, 2009 12:21 am

Calculating age in number of years and number of months

Post 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..
iamngk
Forum Commoner
Posts: 50
Joined: Mon Jun 29, 2009 2:20 am

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

Post by iamngk »

you have to convert date into time stamp. that will make easy of your process.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
sheikhmdirfan
Forum Newbie
Posts: 22
Joined: Tue Jan 20, 2009 12:21 am

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

Post by sheikhmdirfan »

Thanks Sir...

I had done the same myself already...

Still Thanks a lot for ur help...
DAVID87
Forum Commoner
Posts: 29
Joined: Fri Jun 26, 2009 9:56 am
Location: Nottingham, UK

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

Post 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?
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

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

Post by Eric! »

Take a look at the code I posted here

viewtopic.php?f=1&t=102259
Post Reply