php - 将日期增加一个月

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

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

问候。

tarique asked 2019-09-10T11:19:58Z
14个解决方案
136 votes
$time = strtotime("2010.12.11");
$final = date("Y-m-d", strtotime("+1 month", $time));

// Finally you will have the date you're looking for.
Raphael Caixeta answered 2019-09-10T11:20:05Z
30 votes

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

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;
    }

例:

$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
Jason answered 2019-09-10T11:20:31Z
26 votes

使用DateTime::add

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

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

Matthew Flaschen answered 2019-09-10T11:21:04Z
13 votes
strtotime( "+1 month", strtotime( $time ) );

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

Galen answered 2019-09-10T11:21:28Z
5 votes

我用这种方式: -

 $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;
//******************************************************************//
vineet answered 2019-09-10T11:21:56Z
5 votes

你可以使用DateTime::modify这样:

$date = new DateTime('2010-12-11');
$date->modify('+1 month');

见文件:

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

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

HRoux answered 2019-09-10T11:22:43Z
4 votes
(date('d') > 28) ? date("mdY", strtotime("last day of next month")) : date("mdY", strtotime("+1 month"));

这将弥补2月和其他31天的月份。 你当然可以做更多的检查,以便更准确地获得“下个月的这一天”相对日期格式(这不会令人遗憾,见下文),你也可以使用DateTime。

无论下个月的天数如何,DateInterval('P1M')strtotime("+1 month")都基本上盲目地添加31天。

  • 2010-01-31 => 3月3日
  • 2012-01-31 => 3月2日(闰年)
Wayne Weibel answered 2019-09-10T11:23:29Z
3 votes

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

使用此功能后,它正常工作;

$date =  date('d-m-Y',strtotime("12-12-2012 +2 Months");

这里12-12-2012是你的日期,+ 2个月是这个月的增量;

您还可以增加年份,日期

strtotime("12-12-2012 +1 Year");

Ans是12-12-2013

Pravin Suthar answered 2019-09-10T11:24:19Z
1 votes
$date = strtotime("2017-12-11");
$newDate = date("Y-m-d", strtotime("+1 month", $date));

如果你想增加天数,你也可以这样做

$date = strtotime("2017-12-11");
$newDate = date("Y-m-d", strtotime("+5 day", $date));
Kapil Kumar answered 2019-09-10T11:24:44Z
0 votes

谢谢杰森,你的帖子非常有帮助。 我重新格式化了它并添加了更多评论以帮助我理解这一切。 如果有人帮忙,我在这里发布:

function cycle_end_date($cycle_start_date, $months) {
    $cycle_start_date_object = new DateTime($cycle_start_date);

    //Find the date interval that we will need to add to the start date
    $date_interval = find_date_interval($months, $cycle_start_date_object);

    //Add this date interval to the current date (the DateTime class handles remaining complexity like year-ends)
    $cycle_end_date_object = $cycle_start_date_object->add($date_interval);

    //Subtract (sub) 1 day from date
    $cycle_end_date_object->sub(new DateInterval('P1D')); 

    //Format final date to Y-m-d
    $cycle_end_date = $cycle_end_date_object->format('Y-m-d'); 

    return $cycle_end_date;
}

//Find the date interval we need to add to start date to get end date
function find_date_interval($n_months, DateTime $cycle_start_date_object) {
    //Create new datetime object identical to inputted one
    $date_of_last_day_next_month = new DateTime($cycle_start_date_object->format('Y-m-d'));

    //And modify it so it is the date of the last day of the next month
    $date_of_last_day_next_month->modify('last day of +'.$n_months.' month');

    //If the day of inputted date (e.g. 31) is greater than last day of next month (e.g. 28)
    if($cycle_start_date_object->format('d') > $date_of_last_day_next_month->format('d')) {
        //Return a DateInterval object equal to the number of days difference
        return $cycle_start_date_object->diff($date_of_last_day_next_month);
    //Otherwise the date is easy and we can just add a month to it
    } else {
        //Return a DateInterval object equal to a period (P) of 1 month (M)
        return new DateInterval('P'.$n_months.'M');
    }
}

$cycle_start_date = '2014-01-31'; // select date in Y-m-d format
$n_months = 1; // choose how many months you want to move ahead
$cycle_end_date = cycle_end_date($cycle_start_date, $n_months); // output: 2014-07-02
Greg answered 2019-09-10T11:25:09Z
0 votes
function dayOfWeek($date){
    return DateTime::createFromFormat('Y-m-d', $date)->format('N');
}

用法示例:

echo dayOfWeek(2016-12-22);
// "4"
echo dayOfWeek(date('Y-m-d'));
// "4"
T30 answered 2019-09-10T11:25:33Z
0 votes

对于任何寻找任何日期格式的答案的人。

echo date_create_from_format('d/m/Y', '15/04/2017')->add(new DateInterval('P1M'))->format('d/m/Y');

只需更改日期格式即可。

Fabricio answered 2019-09-10T11:26:03Z
-1 votes

在输入框中输入日期,然后在jquery中单击日期从日期获取日期

$(document).ready( function() {
    $("button").click(function(){   
    var day = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
    var a = new Date();
    $(".result").text(day[a.getDay()]);

    });  
             });
user9401222 answered 2019-09-10T11:26:29Z
-1 votes
 <?php
              $selectdata ="select fromd,tod  from register where username='$username'";
            $q=mysqli_query($conm,$selectdata);
            $row=mysqli_fetch_array($q);

            $startdate=$row['fromd']; 
            $stdate=date('Y', strtotime($startdate));  

            $endate=$row['tod']; 
            $enddate=date('Y', strtotime($endate));  

            $years = range ($stdate,$enddate);
            echo '<select name="years" class="form-control">';
            echo '<option>SELECT</option>';
            foreach($years as $year)
              {   echo '<option value="'.$year.'"> '.$year.' </option>';  }
                echo '</select>'; ?>
Sasi Kumar S answered 2019-09-10T11:26:47Z
translate from https://stackoverflow.com:/questions/2870295/increment-date-by-one-month