php 下个月起始结束日期

2025-04-13 21:47:24
推荐回答(3个)
回答1:

$now = time();
$now_m = date("m", $now);
$next_line = $now + 28 * 60 * 60 * 24 - 1;
if(date("m", $next_line ) == $now_m ){
$first = date("Ymd", strtotime(date("Y-m-1", $next_line )));
$last = date("Ymd", strtotime(date("Y-m-28", $next_line )));
}else if(date("m", $next_line + 60 * 60 * 24 ) == $now_m){
$first = date("Ymd", strtotime(date("Y-m-1", $next_line + 60 * 60 * 24 )));
$last = date("Ymd", strtotime(date("Y-m-29", $next_line + 60 * 60 * 24 )));
}else if(date("m", $next_line + 60 * 60 * 24 * 2 ) == $now_m){
$first = date("Ymd", strtotime(date("Y-m-1", $next_line + 60 * 60 * 24 * 2 )));
$last = date("Ymd", strtotime(date("Y-m-30", $next_line + 60 * 60 * 24 * 2 )));
}else if(date("m", $next_line + 60 * 60 * 24 * 3 ) == $now_m){
$first = date("Ymd", strtotime(date("Y-m-1", $next_line + 60 * 60 * 24 * 3 )));
$last = date("Ymd", strtotime(date("Y-m-31", $next_line + 60 * 60 * 24 * 3 )));
}

这里为了演示所以直接把那些相乘计算分开写了,写到程序里时建议直接写结果,减少程序执行时间,这个程序可以封成一个方法,传入一个时间戳就可以获得指定时间的下个月的头天和最后一天了。

回答2:

function DateAdd($part, $number, $date)
{
$date_array = getdate(strtotime($date));
$hor = $date_array["hours"];
$min = $date_array["minutes"];
$sec = $date_array["seconds"];
$mon = $date_array["mon"];
$day = $date_array["mday"];
$yar = $date_array["year"];
switch($part)
{
case "y": $yar += $number; break;
case "q": $mon += ($number * 3); break;
case "m": $mon += $number; break;
case "w": $day += ($number * 7); break;
case "d": $day += $number; break;
case "h": $hor += $number; break;
case "n": $min += $number; break;
case "s": $sec += $number; break;
}
return date("Y-m-d H:i:s", mktime($hor, $min, $sec, $mon, $day, $yar));
}

$today = "2015-01-30";
$nowMonth = date("m", strtotime($today));
$nowYear = date("Y", strtotime($today));
$nextMonth1 = DateAdd('m', 1, $nowYear."-".$nowMonth."-1");
$nextnextMonth = DateAdd('m', 2, $nowYear."-".$nowMonth."-1");
$nextMonth2 = DateAdd('d', -1, $nextnextMonth );
echo "下月开始日期:".date("Ymd",strtotime($nextMonth1));
echo "
";
echo "下月结束日期:".date("Ymd",strtotime($nextMonth2));
exit();
?>

回答3:

function getdays($day){
    $firstday = date('Y-m-d', strtotime(date('Y-m-01', strtotime($day)) . ' +1 month'));//每个月的开始日期肯定是1号
    $lastday = date('Y-m-d', strtotime(date('Y-m-01', strtotime($day)) . ' +2 month -1 day'));
    return array($firstday,$lastday);

}
print_r(getdays('2015-01-30'));