php mysql两个日期之间的记录数

时间:2014-04-07 08:53:59

标签: php mysql

我有这段代码来获取两个日期$date1$date2

之间的记录
for ($i=$date1;$i<$date2;$i++){//BETWEEN THE TWO DATES
        $count_rcvd=mysql_num_rows(mysql_query("select * from cards where Date_Rcvd='$i'"));
        $count_collect=mysql_num_rows(mysql_query("select * from cards where Date_Collected='$i'"));
        if($i==$date1){FOR THE FIRST DATE IN THE RANGE
        $uncollected=mysql_num_rows(mysql_query("select * from cards where Status='AVAILABLE' and Date_Rcvd<='$i'"));
        $balance=$uncollected;
    }
    else{//FOR CONSECUTIVE DATES AFTER DATE1
    $uncollected=mysql_num_rows(mysql_query("select * from cards where Status='AVAILABLE' and Date_Rcvd<'$i'"));//previous days balance
    $balance=$uncollected+$count_rcvd-$count_collect;

当我回应$ i时       echo&#34;&#34 ;;       echo $ i;
      echo&#34;&#34 ;;       echo $ count_rcvd;       echo&#34;&#34 ;;       echo $ count_collect;       echo&#34;&#34 ;;       echo $ balance;       echo&#34;&#34;;

我在下面的日期远远超出了日历日期

014-03-15   0   7   103
2014-03-16  0   3   100
2014-03-17  0   7   96
2014-03-18  0   9   94
2014-03-19  0   5   98
2014-03-20  15  6   112
2014-03-21  0   6   103
2014-03-22  0   1   108
2014-03-23  0   0   109
2014-03-24  0   8   101
2014-03-25  0   3   106
2014-03-26  0   0   109
2014-03-27  1   2   108
2014-03-28  0   0   110
2014-03-29  6   1   115
2014-03-30  0   0   116
2014-03-31  0   0   116
2014-03-32  0   0   116
2014-03-33  0   0   116
2014-03-34  0   0

问题是输出日期超过31,例如....帮助!!!

1 个答案:

答案 0 :(得分:0)

要迭代日期,我建议您使用DateTime和DateTime Interval类(注意!PHP 5.3最小值):http://www.php.net/manual/fr/class.datetime.php

您可以在真实日历后添加天/月:http://www.php.net/manual/fr/datetime.add.php