note 11462 added to date
| From: | eric at emgcomputing dot com | Date: | Wed, 21 Feb 2001 21:11:38 +0000 |
| Subject: | note 11462 added to date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-11377@lists.php.net to get a copy of this message | ||
To calculate the age difference between two dates in yyyy-mm-dd format
function get_age($date1,$date2) {
# Make sure the 1st date is the earliest date
if(strtotime($date1)>strtotime($date2)) {
$tmp=$date1;
$date1=$date2;
$date2=$tmp;
}
$year1=date("Y",strtotime($date1));
$month1=date("n",strtotime($date1));
$day1=date("j",strtotime($date1));
$year2=date("Y",strtotime($date2));
$month2=date("n",strtotime($date2));
$day2=date("j",strtotime($date2));
# Calc # of months until the end of the year
$months=12-$month1;
# Calc # years between dates
$years=$year2-$year1-1;
# Calc # of days remainder
if($day2>=$day1) {
$days=$day2-$day1;
$months=$months+$month2;
} else {
$days=($diff=date("j",mktime(0,0,0,$month2,0,$year2))-$day1)>0?$diff+$day2:$day2;
$months=$months+$month2-1;
}
# Adjust for year boundary
$years+=$months>=12?1:0;
# Get the month number
$months=$months%12;
$age["years"]=$years;
$age["months"]=$months;
$age["days"]=$days;
return $age;
}
$age=array();
$age=get_age("2001-12-03","1968-09-15");
echo "years=".$age["years"]."<BR>";
echo "months=".$age["months"]."<BR>";
echo "days=".$age["days"]."<BR>";
$age=get_age("1973-06-08","2001-02-15");
echo "years=".$age["years"]."<BR>";
echo "months=".$age["months"]."<BR>";
echo "days=".$age["days"]."<BR>";
http://www.php.net/manual/en/function.date.php