php 给定时间戳 加一月 如果下一月天数不够就返回下一月最后一天

function plusOneMonth($time){
    //$time = time();
    $date = date("Y-m-d",$time);
    $day = date("d",$time);
    
    if($day=="29" || $day=="30" || $day=="31"){
        //获取下月最后一天
        $BeginDate=date('Y-m-01', strtotime(date("Y-m-d",$time)." +1 month"));   
           $next_month_day = date('d', strtotime("$BeginDate -1 day"));
        
        //如果下月最后一天大于等于$day,正常返回
        //如果下月最后一天小于$day,返回下月最后一天
        if($next_month_day>=$day){
            return strtotime(date('Y-m-'.$day, strtotime("$BeginDate -1 day")));
        }else{
            return strtotime(date('Y-m-d', strtotime("$BeginDate -1 day")));
        }
        
    }else{
        return strtotime(date("Y-m-d",strtotime("$date +1 month")));
    }
    
}
原文地址:https://www.cnblogs.com/zonglonglong/p/13440581.html