计算两个日期相差年月日

 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 }
<?php  
  //Get detail gap of year,month and days between two different time by vfhky 20130728  
  $common = (time()-strtotime(get_option('swt_builddate')));  
  $a = floor($common/86400/360);    //整数年  
  $b = floor($common/86400/30) - $a*12; //整数月  
  $c = floor($common/86400) - $a*360 - $b*30;   //整数日  
  $d = floor($common/86400);    //总的天数  
  echo $a."年".$b."月".$c."日(共计".$d."天)";  
?> 

 

posted @ 2016-01-18 18:41  Bit5566  阅读(1380)  评论(0编辑  收藏  举报