$startMonth = strtotime(2011-02-20);
$endMonth = strtotime(2011-06-05);
while($startMonth <= $endMonth)
{
echo date('F Y', $startMonth);
$startMonth = strtotime("+1 month", $startMonth);
}
我在使用上面的代码获取开始日期和结束日期之间的数据时遇到了问题。如果我运行这段代码,它只能够获取02(feb(到05(april(之间的数据,而06(june(则被省略。我只是想知道我的代码有什么问题。如果我给出日期为2011-02-01至2011-06-01,则生成输出。
问题是:
如果我给:"2011-02-01"到"2011-06-01",它工作
如果我把"2011-02-02"改为"2011-02-01">它将不起作用
如果我把"2011-02-02"改为"2011-02-03",它可以
替换
$startMonth = strtotime(2011-02-20);
$endMonth = strtotime(2011-06-05);
带有
$startMonth = strtotime('2011-02-20');
$endMonth = strtotime('2011-06-05');
您需要清楚地设置日期是字符串值,因为PHP进行计算并将2011-02-20替换为1989(2011减去2减去20(
---已添加---
好的,还有一个变体(希望我能理解你想看到的(
<?php
$startMonth = strtotime(date("01-n-Y",strtotime('2011-02-20')));
$endMonth = strtotime(date('01-n-Y',strtotime('2011-06-05')));
while($startMonth <= $endMonth)
{
echo date('F Y', $startMonth);
$startMonth = strtotime("+1 month", $startMonth);
}
?>
您应该在$2011删除$sign$2011年至2011年。这将解决您的问题
$startMonth = strtotime(2011-02-20);
$endMonth = strtotime(2011-06-05);
while($startMonth <= $endMonth)
{
echo date('F Y', $startMonth);
$startMonth = strtotime("+1 month", $startMonth);
}
您在不需要的地方还有额外的$。
$2011-02-20
应该是
2011-02-20
如果我们认为这是印刷错误,2011-06-20美元然后
$startMonth+4个月=2011-06-20
2011-06-20 > 2011-06-05
因此echo不打印2011年6月