加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

日期加上一个月的PHP

发布时间:2020-12-13 14:14:03 所属栏目: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日,
参见英文答案 > 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读