日期加上一个月的PHP

前端之家收集整理的这篇文章主要介绍了日期加上一个月的PHP前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > PHP: Adding months to a date,while not exceeding the last day of the month4个
我在PHP中的日期有一个小问题.

当我1月31日1月
用这个代码

$newDate = date('Y-m-d',strtotime('31-01-2016'.' + 1 month'));
echo $newDate;

它给了我3月2日,但我需要在2月29日给我,

我需要加1个月而不是30天.

同上所有日期:
例如
1月1日1月=> 2月1日

1月29日1月=> 2月29日

1月30日1个月=> 2月29日

1月31日1月=> 2月29日

谢谢您帮忙

我想你正在寻找这种类型的约会.
<?PHP
    $date = date('2016-01-31');
    $currentMonth = date("m",strtotime($date));
    $nextMonth = date("m",strtotime($date."+1 month"));
    if($currentMonth==$nextMonth-1){
        $nextDate = date('Y-m-d',strtotime($date." +1 month"));
    }else{
        $nextDate = date('Y-m-d',strtotime("last day of next month",strtotime($date)));
    }
    echo "Next date would be : $nextDate";
?>

查看现场演示:https://eval.in/610034

>如果日期是31-01-2016,那么下一个日期将是29-02-2016>如果日期是25-01-2016,那么下一个日期将是25-02-2016

原文链接:https://www.f2er.com/php/130636.html

猜你在找的PHP相关文章