php 将日期增加一个月
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2870295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
increment date by one month
提问by tarique
Let's say I have a date in the following format: 2010-12-11 (year-mon-day)
假设我有以下格式的日期:2010-12-11 (year-mon-day)
With PHP, I want to increment the date by one month, and I want the year to be automatically incremented, if necessary (i.e. incrementing from December 2012 to January 2013).
使用 PHP,我想将日期增加一个月,并且我希望年份在必要时自动增加(即从 2012 年 12 月增加到 2013 年 1 月)。
Regards.
问候。
回答by Raphael Caixeta
$time = strtotime("2010.12.11");
$final = date("Y-m-d", strtotime("+1 month", $time));
// Finally you will have the date you're looking for.
回答by Jason
I needed similar functionality, except for a monthly cycle (plus months, minus 1 day). After searching S.O. for a while, I was able to craft this plug-n-play solution:
我需要类似的功能,除了每月周期(加上几个月,减去 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;
}
Example:
例子:
$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
回答by Matthew Flaschen
Use DateTime::add.
$start = new DateTime("2010-12-11", new DateTimeZone("UTC"));
$month_later = clone $start;
$month_later->add(new DateInterval("P1M"));
I used clonebecause add modifies the original object, which might not be desired.
我使用clone是因为 add 修改了原始对象,这可能不是我们想要的。
回答by Galen
strtotime( "+1 month", strtotime( $time ) );
this returns a timestamp that can be used with the date function
这将返回一个可以与日期函数一起使用的时间戳
回答by Wayne Weibel
(date('d') > 28) ? date("mdY", strtotime("last day of next month")) : date("mdY", strtotime("+1 month"));
This will compensate for February and the other 31 day months. You could of course do a lot more checking to to get more exact for 'this day next month' relative date formats(which does not work sadly, see below), and you could just as well use DateTime.
这将补偿二月和其他 31 天的月份。您当然可以做更多的检查以获得更准确的“下个月的今天”相对日期格式(遗憾的是它不起作用,见下文),您也可以使用 DateTime。
Both DateInterval('P1M')and strtotime("+1 month")are essentially blindly adding 31 days regardless of the number of days in the following month.
双方DateInterval('P1M')并strtotime("+1 month")基本上是盲目地在下个月加入31天不管天数。
- 2010-01-31 => March 3rd
- 2012-01-31 => March 2nd (leap year)
- 2010-01-31 => 3 月 3 日
- 2012-01-31 => 3 月 2 日(闰年)
回答by HRoux
You can use DateTime::modifylike this :
你可以这样使用DateTime::modify:
$date = new DateTime('2010-12-11');
$date->modify('+1 month');
See documentations :
请参阅文档:
http://php.net/manual/fr/datetime.modify.php
http://php.net/manual/fr/datetime.modify.php
回答by vineet
I use this way:-
我用这种方式:-
$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;
//******************************************************************//
回答by Pravin Suthar
Please first you set your date format as like 12-12-2012
请首先将日期格式设置为 12-12-2012
After use this function it's work properly;
使用此功能后即可正常使用;
$date = date('d-m-Y',strtotime("12-12-2012 +2 Months");
Here 12-12-2012 is your date and +2 Months is increment of the month;
这里 12-12-2012 是您的日期,+2 Months 是月份的增量;
You also increment of Year, Date
您还增加了年份、日期
strtotime("12-12-2012 +1 Year");
Ans is 12-12-2013
答案是 12-12-2013
回答by Greg
Thanks Jason, your post was very helpful. I reformatted it and added more comments to help me understand it all. In case that helps anyone, I have posted it here:
谢谢杰森,你的帖子很有帮助。我重新格式化了它并添加了更多评论以帮助我理解这一切。如果对任何人有帮助,我已将其发布在这里:
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
回答by Kapil Kumar
$date = strtotime("2017-12-11");
$newDate = date("Y-m-d", strtotime("+1 month", $date));
If you want to increment by days you can also do it
如果你想按天递增,你也可以这样做
$date = strtotime("2017-12-11");
$newDate = date("Y-m-d", strtotime("+5 day", $date));

