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函数集分类目录。将固定链接加入收藏夹。

1 则回应给 php计算月份差

  1. Seo Moldova说:

    Thanks for the interesting information. Subscribe to rss

发表评论

电子邮件地址不会被公开。 必填项已用*标注

看不清?