在开始日期到结束日期之间获取数据

时间:2011-06-24 11:51:34

标签: php codeigniter

$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”,那么

4 个答案:

答案 0 :(得分:0)

你应该在$ 2011删除$ sign。 2011年至2011年。 这将解决你的问题

答案 1 :(得分:0)

$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

答案 2 :(得分:0)

如果我们认为这是一个错误的印记$ 2011-06-20 然后

$ startMonth + 4 month = 2011-06-20

2011-06-20 > 2011-06-05

所以echo 不打印 2011年6月

答案 3 :(得分:0)

替换

$startMonth = strtotime(2011-02-20);
$endMonth = strtotime(2011-06-05);

$startMonth = strtotime('2011-02-20');
$endMonth = strtotime('2011-06-05');

你需要清楚地设置日期是字符串值,因为PHP进行计算并用1989(2011减2减20)取代2011-02-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);
     }
?>
相关问题