简体   繁体   中英

number of records between two dates phpmysql

I have this code to get records between two dates $date1 and $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;

When i echo for $i echo ""; echo $i;
echo ""; echo $count_rcvd; echo ""; echo $count_collect; echo ""; echo $balance; echo "";

I get out below with the date going well beyond the calender dates

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

the problem is the output dates go beyond 31st for example....HELP!!!

To iterate on dates, i recommand you to use DateTime and DateTimeInterval classes (Be careful ! PHP 5.3 minimum) : http://www.php.net/manual/fr/class.datetime.php

You can add days/months following the real calendar : http://www.php.net/manual/fr/datetime.add.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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