Page 1 of 1
need help calculating exact age in php
Posted: Tue Dec 08, 2009 1:57 pm
by hectorubie
Hi everyone,
i am trying to get php codes that can calculate exact age from a user. just like you see in myspace, facebook, and all the social networking websites.
the code should take everything in consideration, year, month, day.
for example if i was born 1955-4-24 the code should return my exact age which would be 55 years old.
ive looked through the internet for 4 days and the codes i find would give me the wrong age when i input a date of birth. maybe you guys can help me, THANK YOU IN ADVANCE
Re: need help calculating exact age in php
Posted: Tue Dec 08, 2009 2:26 pm
by AbraCadaver
If you were born 1955-4-24 then you are 54 years old. This will do it:
Code: Select all
(date('Y') - date('Y',strtotime('1955-4-24'));
or
Code: Select all
(int)((time() - strtotime('1955-4-24')) / 31556926);
If you insist upon having 55 or any part of a year count as a yea for whatever reason, then this or similar will do it:
Code: Select all
ceil((time() - strtotime('1955-4-24')) / 31556926);
Or if you want it to round up or down if the date is more or less than half of a year, then:
Code: Select all
round((time() - strtotime('1955-4-24')) / 31556926);
Lot's of ways to do these.
Re: need help calculating exact age in php
Posted: Tue Dec 08, 2009 2:37 pm
by hectorubie
THANK YOU,
the reason why i gave 1955-04-24 is because my mother was born on that day and she is 55 years old.
this year in april she turned 55 and the codes i was using was returning 54 and tha was wrong, it was only calculating the years, not months or days.
can i ask you something, does this codes you gave me take in consideration leap years?
Re: need help calculating exact age in php
Posted: Tue Dec 08, 2009 2:53 pm
by AbraCadaver
hectorubie wrote:THANK YOU,
the reason why i gave 1955-04-24 is because my mother was born on that day and she is 55 years old.
this year in april she turned 55 and the codes i was using was returning 54 and tha was wrong, it was only calculating the years, not months or days.
can i ask you something, does this codes you gave me take in consideration leap years?
If your mother was born 1955-04-24, then this year 2009-04-24 she turned 54 years old and is 54 years old now. Next year on 2010-04-24 she will be 55. If she is indeed 55 now, then she had to have been born in 1954, period.
To get your age, subtract your birth year from today's year, that simple. I was born February 20, 1971 and I am: 2009 - 1971 = 38 years old!
Re: need help calculating exact age in php
Posted: Tue Dec 08, 2009 3:32 pm
by hectorubie
i tried your codes but they work for certain years like the last code you gave me tha will give me the age of 55 thats for that date of birth when i put 1984-03-01 it gives me the age of 26 when it suppose to be 25. im sorry if im bein a pest, really been trying to get this code for a while now. thx for ur help again
AbraCadaver wrote:If you were born 1955-4-24 then you are 54 years old. This will do it:
Code: Select all
(date('Y') - date('Y',strtotime('1955-4-24'));
or
Code: Select all
(int)((time() - strtotime('1955-4-24')) / 31556926);
If you insist upon having 55 or any part of a year count as a yea for whatever reason, then this or similar will do it:
Code: Select all
ceil((time() - strtotime('1955-4-24')) / 31556926);
Or if you want it to round up or down if the date is more or less than half of a year, then:
Code: Select all
round((time() - strtotime('1955-4-24')) / 31556926);
Lot's of ways to do these.
Re: need help calculating exact age in php
Posted: Tue Dec 08, 2009 3:41 pm
by hectorubie
i am goin to kill her cause she assured me tha she was 55. thank you
AbraCadaver wrote:hectorubie wrote:THANK YOU,
the reason why i gave 1955-04-24 is because my mother was born on that day and she is 55 years old.
this year in april she turned 55 and the codes i was using was returning 54 and tha was wrong, it was only calculating the years, not months or days.
can i ask you something, does this codes you gave me take in consideration leap years?
If your mother was born 1955-04-24, then this year 2009-04-24 she turned 54 years old and is 54 years old now. Next year on 2010-04-24 she will be 55. If she is indeed 55 now, then she had to have been born in 1954, period.
To get your age, subtract your birth year from today's year, that simple. I was born February 20, 1971 and I am: 2009 - 1971 = 38 years old!
Re: need help calculating exact age in php
Posted: Tue Dec 08, 2009 3:43 pm
by AbraCadaver
These are correct (both return 54):
Code: Select all
echo date('Y') - date('Y',strtotime('1955-4-24'));
echo (int)((time() - strtotime('1955-4-24')) / 31556926);
These are correct (both return 25):
Code: Select all
echo date('Y') - date('Y',strtotime('1984-03-01'));
echo (int)((time() - strtotime('1984-03-01')) / 31556926);
If you don't agree with the answer they give you, then I'm sorry that you are wrong.
BTW. I was born in 1971, what day and month in 1971 would make me NOT 38?
Re: need help calculating exact age in php
Posted: Tue Dec 08, 2009 3:52 pm
by hectorubie
thank you guys, sorry i was just a pest, you guy were correct,
thank you for all your help.