2016-07-28 162 views
0

在DB中,也有第26天和第31天但没有更改为OK。 仅获得第3天并更改为OK。PHP MYSQL日期范围错过日期

我在哪里错了这段代码?

代码:

*$from = date("Y-m-01"); 
$to  = date("Y-m-t"); // last day current month 
$query = "SELECT date FROM tbl_data WHERE date BETWEEN '$from' AND '$to' order by date DESC"; 
$result = mysqli_query($mysqli,$query); 
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) 
{ 
    $date = date_create_from_format('Y-m-d', $row['date']); 
} 
$cursor = date_create_from_format('Y-m-d', $from); 
$finish = date_create_from_format('Y-m-d', $to); 
while ($cursor != $date) 
    { 
     echo date_format($cursor,'Y-m-d') . "--- Missed <br>"; 
     date_modify($cursor, '+1 day'); 

      while($cursor == $date) 
       { 
        echo date_format($date,'Y-m-d') . "--- OK <br>"; 
        date_modify($cursor, '+1 day'); 
       } 
      while($cursor > $finish) 
       { 
        die(); 
       } 
}* 

输出:

2016年7月1日---错过
2016年7月2日---错过
2016年7月3日---确定
2016年7月4日---错过
2016年7月5日---错过
2016年7月6日---错过
2016年7月7日---错过
2016-07-08 ---错过
2016-07-09 ---错过
2016-07-10 ---错过
2016-07-11 ---错过
2016-07-12 ---错过
2016年7月13日---错过
2016年7月14日---错过
2016年7月15日---错过
2016年7月16日---错过
2016-07-17 ---错过
2016-07-18 ---错过
2016-07-19 ---错过
2016-07-20 ---错过
2016-07-21 ---错过
2016-07-22 ---错过
2016-07-23 ---错过
2016-07-24 ---错过
2016-07-25 ---错过
2016年7月26日---错过
2016年7月27日---错过
2016年7月28日---错过
2016年7月29日---错过
2016-07-30 ---错过
2016-07-31 ---错过

+3

我不明白你想问什么,请详细说明你的问题。 – Janno

+0

不要关闭你的第一个while循环那里,关闭它在最后 – Rijin

+0

没有改变我试过 –

回答

0

你的问题很难理解,但据我所知你正试图检查是否有迭代日期的记录。

您错过的主要观点是,您只在$date变量上存储单个日期。澄清;

while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) 
{ 
    $date = date_create_from_format('Y-m-d', $row['date']); 
} 

/* at this point of code, your variable $date is exactly equal to 2016-07-03 
* because you sorted all existing dates in descending order and last record is 2016-07-03 
*/ 

在此代码部分之后,您的“OK”检查仅对“2016-07-03”有效。要更正代码,您应该有数组来存储所有日期,并使用in_array()函数来检查存在。

另一种方法是在更改ORDER BY方向后,在while循环内部应用这些过程。如果两个连续的记录不是连续的日子,你将填补这些空白(第二级)迭代。