标题稍微有点绕,原谅我语文不好,叙述没清楚,我这里再重新举个例子说一下上边的意思: 假设今天是2016-01-25日 那么我想获得下个月也就是2016-02-25 这个日期,要跟随完整的月份,也就是说假设下个月没有25号的话 我要获得的是下个月的最后一天,写之前网上查了下,没找到!好吧,自己动手丰衣足食!
---------------------------- 实际代码上来之前再啰嗦两句 我现在用的方法可能很直接 直接涉及的函数就是 date 、strtotime、getdate这三个系统函数,只需传入日期(2016-01-25)和几个月数字即可。好了 闲话不多说上代码
private function getNextDate($date,$num){
$now = strtotime($date);
$time=getdate($now);
$new_mon = $time['mon'] + $num;
if($time['mon'] > 12){
$new_mon = 1;
$new_year = $time['year'] + 1;
}else{
$new_year = $time['year'];
}
$str_next_date = $new_year.'-'.$new_mon.'-01';
$str_last_day = date("Y-m-d",strtotime("$str_next_date +1 month -1 day"));
$last_day = strtotime($str_last_day);
$next_time = getdate($last_day);
if($time['mday'] > $next_time['mday']){
$str = $new_year.'-'.$new_mon.'-'.$next_time['mday'];
}else{
$str = $new_year.'-'.$new_mon.'-'.$time['mday'];
}
$int_time = strtotime($str);
return $int_time;
}
类似 输入2016-01-30返回为2016-02-29毕竟2月没有30号 当然了输入2016-01-31 也是返回2016-02-29的