繁体   English   中英

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

[英]Fetching data between Start date to end date

$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”,则可以正常运行

您应在$ 2011处删除$符号。 $ 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

所以回声不打印 2011年6月

更换

$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);
     }
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM