note 45152 modified in function.date by aidan
| From: | aidan@php.net | Date: | Tue, 07 Sep 2004 06:59:31 +0000 |
| Subject: | note 45152 modified in function.date by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-76127@lists.php.net to get a copy of this message | ||
A nice way to calc age seeing that mktime only goes back until 1970 is to use the date function like
this...
<?php
//Find the difference in year, month, and day
$yeardiff = date("Y") - $dobyear;
$monthdiff = date("m") - $dobmonth;
$daydiff = date("j") - $dobday;
/*
* if month or day is negative we have yet to reach it so
* we need to subtract a year seeing we haven't
* reached our birthday yet, else yeardiff is correct
*/
if ($monthdiff <= 0 && $daydiff < 0) {
$age = $yeardiff - 1;
} else {
$age = $yeardiff;
}
echo $age;
?>
--was--
A nice way to calc age seeing that mktime only goes back until 1970 is to use the date function like
this...
//Find the difference in year, month, and day
$yeardiff = date("Y") - $dobyear;
$monthdiff = date("m") - $dobmonth;
$daydiff = date("j") - $dobday;
/*
* if month or day is negative we have yet to reach it so
* we need to subtract a year seeing we haven't
* reached our birthday yet, else yeardiff is correct
*/
if($monthdiff < 0 || $daydiff < 0){
$age = $yeardiff - 1;
}else{
$age = $yeardiff;
}
echo $age;
http://php.net/manual/en/function.date.php