PHP添加1个月到目前为止

4 php date

我有一个函数,返回1个月前的url.

我想显示当前选定的月份,但我不能使用简单的当月,因为当用户点击链接到1个月后选择的月份将会改变并且不会是最新的.

因此,功能将于2012年8月返回

如何制作一个增加1个月的PHP脚本?

到目前为止我已经:

<?php echo strip_tags(tribe_get_previous_month_text()); ?>
Run Code Online (Sandbox Code Playgroud)

Mar*_*c B 9

简单方法:

$next_month = strtotime('august 2012 next month');
Run Code Online (Sandbox Code Playgroud)

更好的方法:

$d = new Date('August 2012');
$next_month = $d->add(new DateInterval('P1M'));
Run Code Online (Sandbox Code Playgroud)

相关文档:strtotime date dateinterval


小智 6

有 3 个选项/答案

     $givendate is the given date (ex. 2016-01-20)

option 1:
        $date1 = date('Y-m-d', strtotime($givendate. ' + 1 month'));

option 2:
        $date2 = date('Y-m-d', strtotime($givendate. ' + 30 days'));

option 3:
        $number = cal_days_in_month(CAL_GREGORIAN, date('m', strtotime($givendate)), date('Y', strtotime($givendate)));
        $date3 = date('Y-m-d', strtotime($date2. ' + '.$number.' days'));
Run Code Online (Sandbox Code Playgroud)