如果我有一个开始日期(比如说2009-02-01
)和结束日期(比如2010-01-01
),如何创建一个循环来遍历该范围内的所有日期(月份)?
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);
}
注意备注http://php.net/manual/de/datetime.formats.relative.php http://php.net/manual/de/datetime.formats.relative.php
相对月份值是根据月份的长度计算的。例如,“+2 月 2011-11-30”将生成“2012-01-30”。这是因为 11 月有 30 天,12 月有 31 天,总共 61 天。
从 PHP5.3 开始你可以使用http://www.php.net/manual/en/class.dateperiod.php http://www.php.net/manual/en/class.dateperiod.php
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)