PHP一个月后得到一个日期

Bug*_*loo 6 php

<?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);
Run Code Online (Sandbox Code Playgroud)

http://codepad.org/bHiNFIBR

我怎样才能获得正确的日期+1个月我尝试了这些选项.所有人都在5月1日返回但我需要4月30日

Sve*_*lav 12

尝试类似的东西......

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);
Run Code Online (Sandbox Code Playgroud)

但请注意,当你在3月31日为+ 1个月时,正确的目标是5月1日而不是4月30日:)


pon*_*ste 5

我建议这样做:

$next_month = date('Y-m-d', strtotime("+1 months", strtotime("NOW"))); 
Run Code Online (Sandbox Code Playgroud)

编辑

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");
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助 :-)