note 113800 deleted from function.date by peehaa
| From: | peehaa@php.net | Date: | Wed, 04 Dec 2013 13:36:05 +0000 |
| Subject: | note 113800 deleted from function.date by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-197738@lists.php.net to get a copy of this message | ||
Note Submitter: espertalhao04 at hotmail dot com
----
Simple function to calculate a person's age.
You simply provide the day, month and year and it gives the age.
Since it is simple, it won't count for negative numbers.
<?php
function age($d=1,$m=1,$y=1970)
{
$n=explode(' ',date('d m Y'));
if($n[2]<$y)return 0;
return $n[2]-$y-1+(($n[1]=$m&&$n[0]>=$d)||($n[1]>$m));
}
?>
For today (04-12-2013), if you run:
<?php
age(4,12,1992);
?>
You will get 21.
If you run:
<?php
age(5,12,1992);
?>
You will get 20.
This function uses the date() function. Results will vary according to your timezone.