php计算月份差

/*
 *取某个日期到现在时间的月份差值。如果不够一个月,按一个月算
 */
function diffMonth($date){
        if(strtotime($date) < time() ){
                $start = $date;
                $end  = date('Y-n-j');
        }else{
                $start = date('Y-n-j');
                $end  = $date;
        }
        $starY = date("Y",strtotime($start));
        $starM = date("n",strtotime($start));
        $starD = date("j",strtotime($start));
	
        $nowY = date("Y",strtotime($end));
        $nowM = date("n",strtotime($end));
        $nowD = date("j",strtotime($end));
	
        $diffM = 0;
	
        if($starY == $nowY){
                if($starM == $nowM){
                        if($starD < $nowD){
                                $diffM = 1;
                        }elseif($starD = $nowD){
                                $diffM = 0;
                        }else{
                                $diffM = false;
                        }
                }elseif($starM < $nowM){
                        if($starD < $nowD){
                                $diffM = $nowM - $starM + 1;
                        }else{
                                $diffM = $nowM - $starM;
                        }
                }else{
                        $diffM = false;
                }
        }elseif($starY < $nowY){
                $diffY = $nowY - $starY;
                if($starD < $nowD){
                        $diffM = (12 - $starM + $nowM + 1) + 12 * ($diffY - 1);
                }else{
                        $diffM = (12 - $starM + $nowM) + 12 * ($diffY - 1);
                }
	
        }else{
                $diffM = false;
        }
	
        return $diffM;
}
此条目发表在php函数集分类目录。将固定链接加入收藏夹。