如何在PHP中获得下个月

时间:2017-01-31 07:52:14

标签: php

我需要用php来获得下个月。无处不在写这些例子

date('Y-m-t',strtotime("+1 month"))

上述代码的输出是" 2017-03-31"。我需要得到二月而不是三月。

4 个答案:

答案 0 :(得分:5)

如果您想获得下一个,而不管当前月份的当前日期。以下代码可以帮助您

echo date('M',strtotime('first day of +1 month'));

这会给你' 2月'

答案 1 :(得分:3)

像这样使用

// One month from today
$date = date('Y-m-d', strtotime('+1 month'));

// One month from a specific date
$date = date('Y-m-d', strtotime('+1 month', strtotime('2015-01-01')));

答案 2 :(得分:0)

要使用php获取下个月的时间,请使用字符串时间功能:

$nxtm = strtotime("next month");
echo date("M", $nxtm);

答案 3 :(得分:0)

如果您想拥有与本月同一天的下个月,或者如果要切换月份则使用下个月的最后一天,则可以使用此功能

function nextMonth()
{
    $nextMonthNumber = date('M', strtotime('first day of +1 month'));
    $nextMonthDate = new DateTime();
    $nextMonthDate->add(new DateInterval('P1M'));
    while ($nextMonthDate->format('M') != $nextMonthNumber) {
        $nextMonthDate->sub(new DateInterval('P1D'));
    }
    return $nextMonthDate;
}