469,366 Members | 2,254 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

Perfect age calculator

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!

Regards,

Marcel
Jul 17 '05 #1
4 22924
Am Thu, 07 Jul 2005 22:59:56 +0200 schrieb Marcel:
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!

Regards,

Marcel


The script is written in 2 minutes:
<?php
$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))
{
$age--;
}
?>

--
-------------------------------------------------------
Try this: SCA the Smart Class Archive for PHP
http://www.project-sca.org
-------------------------------------------------------

Jul 17 '05 #2
On Thu, 07 Jul 2005 22:59:56 +0200, Marcel wrote:
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.


<?php
print "You're too old!\n";
?>
--
http://www.mgogala.com

Jul 17 '05 #3

"Markus L." <no****@project-sca.org> schreef in bericht
news:pa****************************@project-sca.org...
Am Thu, 07 Jul 2005 22:59:56 +0200 schrieb Marcel:
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!

Regards,

Marcel


The script is written in 2 minutes:
<?php
$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))
{
$age--;
}
?>

--


Thank you!!!
Jul 17 '05 #4
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);
return(floor((time()-$then)/31556926));
}
?>

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. ;-)

--imaek

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by mwh | last post: by
3 posts views Thread by Paul | last post: by
3 posts views Thread by PieMan2004 | last post: by
24 posts views Thread by firstcustomer | last post: by
Deathwing
5 posts views Thread by Deathwing | last post: by
3 posts views Thread by mandy335 | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.