PHP:循环遍历日期范围内的所有月份?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2155110/
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
PHP: Loop through all months in a date range?
提问by JD Isaacks
If I have a start date (say 2009-02-01) and an end date (say 2010-01-01), how can I create a loop to go through all the dates (months) in the range?
如果我有一个开始日期(比如2009-02-01)和一个结束日期(比如2010-01-01),我如何创建一个循环来遍历范围内的所有日期(月)?
回答by Gordon
Try
尝试
$start = $month = strtotime('2009-02-01');
$end = strtotime('2011-01-01');
while($month < $end)
{
echo date('F Y', $month), PHP_EOL;
$month = strtotime("+1 month", $month);
}
Mind the note http://php.net/manual/de/datetime.formats.relative.php
请注意http://php.net/manual/de/datetime.formats.relative.php
Relative month values are calculated based on the length of months that they pass through. An example would be "+2 month 2011-11-30", which would produce "2012-01-30". This is due to November being 30 days in length, and December being 31 days in length, producing a total of 61 days.
相对月份值是根据它们经过的月份长度计算的。一个例子是“+2 月 2011-11-30”,它会产生“2012-01-30”。这是由于 11 月的长度为 30 天,而 12 月的长度为 31 天,总共产生 61 天。
As of PHP5.3 you can use http://www.php.net/manual/en/class.dateperiod.php
从 PHP5.3 开始,您可以使用http://www.php.net/manual/en/class.dateperiod.php
回答by Glavi?
Example of DateTime, DateIntervaland DatePeriodclass combination :
DateTime、DateInterval和DatePeriod类组合的示例:
$start = new DateTime('2009-02-01');
$interval = new DateInterval('P1M');
$end = new DateTime('2011-01-01');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
echo $dt->format('F Y') . PHP_EOL;
}
回答by 3s2ng
The accepted answer is not the proper way.
接受的答案不是正确的方法。
I tried this snippet and it does not work properly. If your start date is the end of the month and the end date is the start of the 3rd month.
我试过这个片段,但它不能正常工作。如果您的开始日期是月底,而结束日期是第三个月的开始。
For example: 2014-08-31 - 2014-10-01
例如:2014-08-31 - 2014-10-01
Expected should be.
预计应该是。
- August
- September
- October
- 八月
- 九月
- 十月
The better solution is:
更好的解决方案是:
$start = new DateTime('2010-12-02');
$start->modify('first day of this month');
$end = new DateTime('2012-05-06');
$end->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
echo $dt->format("Y-m") . "<br>\n";
}
Reference: How to list all months between two dates
回答by user2643679
$start = strtotime('2011-09-01');
$end = strtotime('2013-12-01');
while($start < $end)
{
echo date('F Y', $start) . '<br>';
$start = strtotime("+1 month", $start);
}
回答by Mark
I like the simplicity of the accepted answer, but as 3s2ng, it doesn't always work. So I tweeked it like this:
我喜欢接受答案的简单性,但作为 3s2ng,它并不总是有效。所以我把它改成这样:
$start = strtotime('2009-02-01');
$startmoyr = date('Y', $start) . date('m', $start);
$end = strtotime('2013-12-01');
$endmoyr = date('Y', $end) . date('m', $end);
while ($startmoyr <= $endmoyr) {
echo date("F Y", $start) . "<br>";
$start = strtotime("+1month", $start);
$startmoyr = date('Y', $start) . date('m', $start);
}
回答by pollux1er
I have a method which is optimal in results :
我有一个结果最佳的方法:
$begin = new DateTime( '2014-07-14' );
$end = new DateTime( '2014-08-01' );
$end = $end->modify( '+1 month' );
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($begin, $interval, $end);
foreach($period as $dt) {
var_dump($dt->format( "m" ));
}
A plus for the method of @Glavic
@Glavic 方法的加分项
回答by manuman94
Based on Gordon's response this is the way I actually work when you need to get all the months between.
根据戈登的回应,当您需要在.
$end = strtotime(date("Y-m-01"));
$start = $month = strtotime("-12 months", $end);
while ( $month < $end ) {
echo date("Y-m-d", $month));
$month = strtotime("+1 month", $month);
}
This is the result if I execute this code now:
如果我现在执行此代码,结果如下:
2018-05-01
2018-06-01
2018-07-01
2018-08-01
2018-09-01
2018-10-01
2018-11-01
2018-12-01
2019-01-01
2019-02-01
2019-03-01
2019-04-01
Notice that this doesn't include the current month. If you need to include current month, you can set the "$end" variable to the first day of the next month.
请注意,这不包括当前月份。如果需要包含当月,可以将“$end”变量设置为下个月的第一天。
$current_first_day_of_the_month = date("Y-m-01");
$end = strtotime("$current_first_day_of_the_month +1 month");
$start = $month = strtotime("-12 months", $end);
Hope this helps, greetings.
希望这有帮助,问候。

