1 /** 2 +---------------------------------------------------------- 3 * 功能:计算两个日期相差 年 月 日 4 +---------------------------------------------------------- 5 * @param date $date1 起始日期 6 * @param date $date2 截止日期日期 7 +---------------------------------------------------------- 8 * @return array 9 +---------------------------------------------------------- 10 */11 function DiffDate($date1, $date2) { 12 if (strtotime($date1) > strtotime($date2)) { 13 $ymd = $date2; 14 $date2 = $date1; 15 $date1 = $ymd; 16 } 17 list($y1, $m1, $d1) = explode('-', $date1); 18 list($y2, $m2, $d2) = explode('-', $date2); 19 $y = $m = $d = $_m = 0; 20 $math = ($y2 - $y1) * 12 + $m2 - $m1; 21 $y = floor($math / 12); 22 $m = intval($math % 12); 23 $d = (mktime(0, 0, 0, $m2, $d2, $y2) - mktime(0, 0, 0, $m2, $d1, $y2)) / 86400; 24 if ($d < 0) { 25 $m -= 1; 26 $d += date('j', mktime(0, 0, 0, $m2, 0, $y2)); 27 } 28 $m < 0 && $y -= 1; 29 return array($y, $m, $d); 30 }