Perfect age calculator

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hello all,

I am looking for a perfect PHP age calculator which takes someones date of  
birth as an argument and returns the person's age.

I went over several tutorials, help and newsgroups an found lots of  
calculators, tried many times to make the perfect script but did not  
find/make the perfect one.

Simple test:

if my birthdate is 07-07-2004 (dd-mm-yyyy) and today is 07-07-2005 then i  
should be 1 year old

if my birthdate is 07-07-2004 (dd-mm-yyyy) and today is 06-07-2005 then i  
should be 0 years old

Lots of scripts i've found did not pass this test succesfull, so if someone  
can point me to the 'golden' script then i would be very thankfull!



Re: Perfect age calculator

Am Thu, 07 Jul 2005 22:59:56 +0200 schrieb Marcel:

Quoted text here. Click to load it

The script is written in 2 minutes:
$birthday = '07-07-2004';
$today = date('d-m-Y');

$a_birthday = explode('-', $birthday);
$a_today = explode('-', $today);

$day_birthday = $a_birthday[0];
$month_birthday = $a_birthday[1];
$year_birthday = $a_birthday[2];
$day_today = $a_today[0];
$month_today = $a_today[1];
$year_today = $a_today[2];

$age = $year_today - $year_birthday;

if (($month_today < $month_birthday) || ($month_today == $month_birthday && $day_today < $day_birthday))

Try this: SCA the Smart Class Archive for PHP

Re: Perfect age calculator

Quoted text here. Click to load it

Thank you!!!  

Re: Perfect age calculator

A much more efficient method would just use unix time; the drawback,
however, is that it only goes up to 1970.
An example
getAge($year,$month,$day) {
  $then = mktime(1,1,1,$year,$month,$day);

This should return a round number (e.g. 15, 20, 25) but if you want it
to return the decimal form of the year that has been completed, remove
the floor() function, which, if the user was exactly 20 and 1/2, would
return 20.5, but most likely would return their age and a long string
of decimals (20.324325443545634643545...)

Not that that really answeres your question at all, but I had just been
thinking about this for a while this afternoon, and when I saw this
topic (an excuse to type it up and fully reason it out,) I decided to
jump at it. ;-)


Re: Perfect age calculator

On Thu, 07 Jul 2005 22:59:56 +0200, Marcel wrote:

Quoted text here. Click to load it

 print "You're too old!\n";

Site Timeline