douzhiba6873 2011-06-24 11:51
浏览 25
已采纳

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

$startMonth = strtotime(2011-02-20);
$endMonth = strtotime(2011-06-05);
while($startMonth <= $endMonth)
{
     echo date('F Y', $startMonth);
     $startMonth = strtotime("+1 month", $startMonth);
}

I have a problem while fetching data between start and end date using the code above. If i run this code it's just able to fetch the data between 02[feb] to 05[april] and the 06[june] is omitted. I just want to know what problem is with my code. If i give the dates as 2011-02-01 to 2011-06-01 the output is generated.

problem is like:

  • if i give: '2011-02-01' to '2011-06-01' it works

  • if i give '2011-02-02' to '2011-02-01' it won't work

  • if i give '2011-02-02' to '2011-02-03' it works

  • 写回答

4条回答 默认 最新

  • duanjian4698 2011-06-24 12:25
    关注

    replace

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

    with

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

    You need to clearly set that date is string value, because PHP makes calculation and replaces 2011-02-20 with 1989 (2011 minus 2 minus 20)

    ---added---

    Ok, one more variant (hope I understand what you want to see )

    <?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);
         }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?