遍历日期范围内的所有月份?
如果我有开始日期(例如 2009-02-01
)和结束日期(例如 2010-01-01
),如何创建循环经历范围内的所有日期(月)?
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?
尝试
$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);
}
注意注释 http://php.net/manual/de/datetime.formats.relative.php
相对月份值是根据它们经过的月份长度来计算的.例如"+2 month 2011-11-30",它将产生"2012-01-30".这是因为11月为30天,12月为31天,总共61天.
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.
从PHP5.3开始,您可以使用 http://www.php.net/manual/en/class.dateperiod.php
As of PHP5.3 you can use http://www.php.net/manual/en/class.dateperiod.php