php 计算时间的年月日

 1 /**
 2      * 计算时间的年月日
 3      * 2016-07-12
 4      */
 5     function diffDate($date1,$date2){ 
 6         $datestart= date('Y-m-d',strtotime($date1));
 7         if(strtotime($datestart)>strtotime($date2)){ 
 8             $tmp=$date2; 
 9             $date2=$datestart; 
10             $datestart=$tmp; 
11          } 
12         list($Y1,$m1,$d1)=explode('-',$datestart); 
13         list($Y2,$m2,$d2)=explode('-',$date2); 
14         $Y=$Y2-$Y1; 
15         $m=$m2-$m1; 
16         $d=$d2-$d1; 
17         if($d<0){ 
18             $d+=(int)date('t',strtotime("-1 month $date2")); 
19             $m--; 
20         } 
21         if($m<0){ 
22             $m+=12; 
23             $y--; 
24         } 
25         if($Y == 0){
26             return $m.'个月'.$d.'天';
27         }elseif($Y == 0 && $m == 0){
28             return $d.'天';
29         }else{
30             return $Y.'年'.$m.'个月'.$d.'天';
31         }
32     } 
33 
34 $catdata="1986-09-28"; //计算时间
35 echo diffDate($catdata,date('Y-m-d',time()));

 

posted @ 2016-07-12 20:26  _DongGe  阅读(478)  评论(0编辑  收藏  举报