|
Posted by Markus L. on 07/08/05 00:25
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
-------------------------------------------------------
[Back to original message]
|