增加日期一个月

tar*_*que 96 php date

假设我的日期格式如下:2010-12-11(year-mon-day)

使用PHP,我希望将日期增加一个月,如果需要,我希望年份自动递增(即从2012年12月到2013年1月递增).

问候.

Rap*_*eta 149

$time = strtotime("2010.12.11");
$final = date("Y-m-d", strtotime("+1 month", $time));

// Finally you will have the date you're looking for.
Run Code Online (Sandbox Code Playgroud)

  • 它不适用于所有日期.例如,2013-05-31将显示7月而不是下个月,即6月. (26认同)
  • 这个答案是危险的,因为它在"月末的最后一天"情景中失败,很难被发现. (4认同)
  • 我正在关注 2014-03-03 的 2014-01-31 原因? (2认同)
  • 这有时确实会打破。@jason 的答案在技术上更正确,因为它考虑了闰年、月份长度等。这应该被标记为正确答案。 (2认同)

Jas*_*son 35

我需要类似的功能,除了每月周期(加上几个月,减去1天).在搜索了一段时间后,我能够制作这个即插即用的解决方案:

function add_months($months, DateTime $dateObject) 
    {
        $next = new DateTime($dateObject->format('Y-m-d'));
        $next->modify('last day of +'.$months.' month');

        if($dateObject->format('d') > $next->format('d')) {
            return $dateObject->diff($next);
        } else {
            return new DateInterval('P'.$months.'M');
        }
    }

function endCycle($d1, $months)
    {
        $date = new DateTime($d1);

        // call second function to add the months
        $newDate = $date->add(add_months($months, $date));

        // goes back 1 day from date, remove if you want same day of month
        $newDate->sub(new DateInterval('P1D')); 

        //formats final date to Y-m-d form
        $dateReturned = $newDate->format('Y-m-d'); 

        return $dateReturned;
    }
Run Code Online (Sandbox Code Playgroud)

例:

$startDate = '2014-06-03'; // select date in Y-m-d format
$nMonths = 1; // choose how many months you want to move ahead
$final = endCycle($startDate, $nMonths); // output: 2014-07-02
Run Code Online (Sandbox Code Playgroud)

  • 太好了,正是我所需要的。感谢您为我节省了很多时间! (3认同)

Mat*_*hen 31

使用DateTime::add.

$start = new DateTime("2010-12-11", new DateTimeZone("UTC"));
$month_later = clone $start;
$month_later->add(new DateInterval("P1M"));
Run Code Online (Sandbox Code Playgroud)

我使用了clone,因为add修改了原始对象,这可能是不可取的.

  • 不起作用.. (new DateTime("2010-01-31", new DateTimeZone("UTC")))->add(new DateInterval("P1M"))->format('Ymd') 结果为 2010- 03-03 (2认同)

Gal*_*len 13

strtotime( "+1 month", strtotime( $time ) );
Run Code Online (Sandbox Code Playgroud)

这将返回可与date函数一起使用的时间戳

  • 与接受的答案相同的问题。不适用于所有字符串。 (3认同)

Pra*_*har 6

请首先将日期格式设置为 12-12-2012

使用此功能后即可正常使用;

$date =  date('d-m-Y',strtotime("12-12-2012 +2 Months");
Run Code Online (Sandbox Code Playgroud)

这里 12-12-2012 是您的日期,+2 Months 是月份的增量;

您还增加了年份、日期

strtotime("12-12-2012 +1 Year");
Run Code Online (Sandbox Code Playgroud)

答案是 12-12-2013


Way*_*bel 5

(date('d') > 28) ? date("mdY", strtotime("last day of next month")) : date("mdY", strtotime("+1 month"));
Run Code Online (Sandbox Code Playgroud)

这将补偿2月和其他31天的月份。当然,您可以做更多的检查来获得“下个月的这一天” 相对日期格式的更准确信息(这很难过,请参见下文),并且您也可以使用DateTime。

双方DateInterval('P1M')strtotime("+1 month")基本上是盲目地在下个月加入31天不管天数。

  • 2010-01-31 => 3月3日
  • 2012-01-31 => 3月2日(le年)

  • “不管下一个月的天数,盲加31天”,绝对正确!(+1)。 (2认同)

vin*_*eet 5

我用这种方式: -

 $occDate='2014-01-28';
 $forOdNextMonth= date('m', strtotime("+1 month", strtotime($occDate)));
//Output:- $forOdNextMonth=02


/*****************more example****************/
$occDate='2014-12-28';

$forOdNextMonth= date('m', strtotime("+1 month", strtotime($occDate)));
//Output:- $forOdNextMonth=01

//***********************wrong way**********************************//
$forOdNextMonth= date('m', strtotime("+1 month", $occDate));
  //Output:- $forOdNextMonth=02; //instead of $forOdNextMonth=01;
//******************************************************************//
Run Code Online (Sandbox Code Playgroud)


HRo*_*oux 5

您可以这样使用DateTime::modify

$date = new DateTime('2010-12-11');
$date->modify('+1 month');
Run Code Online (Sandbox Code Playgroud)

参见文档:

http://php.net/manual/fr/datetime.modify.php

http://php.net/manual/fr/class.datetime.php


小智 5

只需使用简单的方法更新答案即可找到几个月后的日期。由于标记的最佳答案并未给出正确的解决方案。

<?php

    $date = date('2020-05-31');
    $current = date("m",strtotime($date));
    $next = date("m",strtotime($date."+1 month"));
    if($current==$next-1){
        $needed = date('Y-m-d',strtotime($date." +1 month"));
    }else{
        $needed = date('Y-m-d', strtotime("last day of next month",strtotime($date)));
    }
    echo "Date after 1 month from 2020-05-31 would be : $needed";

?>
Run Code Online (Sandbox Code Playgroud)