<?php
echo "\n";
echo $end_date = date('Y-m-d', strtotime('+1 month'));
echo "\n";
$today = date("Y-m-d"); // 2012-01-30
echo $next_month = date("Y-m-d", strtotime("$today +1 month"));
echo "\n";
$end_date = mktime(date("H"), date("i"), date("s"), date("n") + 1, date("j"), date("Y"));
echo "\n" . date('Y-m-d',$end_date);
如何才能获得+1个月的正确日期 我试过这些选择。 所有人都在5月1日返回,但我需要4月30日
答案 0 :(得分:11)
尝试类似的东西..
if( date('d') == 31 || (date('m') == 1 && date('d') > 28)){
$date = strtotime('last day of next month');
} else {
$date = strtotime('+1 months');
}
echo date('Y-m-d', $date);
但请注意,当你在3月31日作为+ 1个月正确定位到5月1日而不是4月30日时:)
答案 1 :(得分:3)
我建议:
$next_month = date('Y-m-d', strtotime("+1 months", strtotime("NOW")));
修改强>
function addMonth($date) {
$date = new DateTime($date);
$day = $date->format('j');
$date->modify("+1 month");
$next_month_day = $date->format('j');
if ($day != $next_month_day)
$date->modify('last day of last month');
return $date;
}
$next_month = addMonth(time());
echo $next_month->format("Y-m-d");
希望这会有所帮助: - )
答案 2 :(得分:1)
试试这个解决方案:
echo "\n";
$today = date("Y-m-d");
$next_month = date("Y-m-d", strtotime("$today +1 month"));
echo "\n";
echo $next_month_last_day = date("Y-m-d",strtotime('-1 second',strtotime($next_month)));