/*
php计算任意两个日期之间的天数
基本思想,算术减法:
cur_date 2009 04 11
last_date - 2008 12 20
---------------------
date_dist 0000 03 21
间隔大约3个月21天---116天
*/
// create function
function date_distance($last_date, $cur_date){
$last_date = $last_date;
$cur_date = $cur_date; // 20090411
$ldy = substr($last_date,0,4); // 获得日期的年份
$ldm = substr($last_date,4,2); // 获得日期的月份
$ldd = substr($last_date,6,2); // 获得日期的日子
$cdy = substr($cur_date,0,4);
$cdm = substr($cur_date,4,2);
$cdd = substr($cur_date,6,2);
if( $cdd < $ldd ) {
$cdd = $cdd + 30;
$cdm --;
}
else if ( $cdm < $ldm ){
$cdm = $cdm + 12;
$cdy --;
}
$distance = ($cdy - $ldy)*365 + ($cdm - $ldm)*30 + $cdd - $ldd;
return $distance;
} // end of function
$last_date = 20081220;
$cur_date = date('Ymd'); // 20090411
/*
echo date('Ymd').'--';
echo $cur_date; // 20090411
*/
$test = date_distance($last_date, $cur_date);
echo '2008-12-20离现在共 '.$test.'天';
// possible output:
A.htc:hover { FONT-WEIGHT: bolder } 2008-12-20离现在共 116天 ?>