简体   繁体   English

PHP MYSQL日期范围错过的日子

[英]PHP MYSQL date range missed days

in DB, there is also 26th and 31st days but not changing to OK. 在DB中,还有26天和31天,但没有更改为OK。 only getting 3rd day and change to OK. 仅获得第三天,然后更改为“确定”。

Where am i wrong in this code? 我在此代码中哪里写错了?

Code: 码:

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

Output: 输出:

2016-07-01--- Missed 2016-07-01 ---错过了
2016-07-02--- Missed 2016-07-02 ---错过了
2016-07-03--- OK 2016-07-03 ---好的
2016-07-04--- Missed 2016-07-04 ---错过
2016-07-05--- Missed 2016-07-05 ---错过
2016-07-06--- Missed 2016-07-06 ---错过了
2016-07-07--- Missed 2016-07-07 ---错过了
2016-07-08--- Missed 2016-07-08 ---错过了
2016-07-09--- Missed 2016-07-09 ---错过了
2016-07-10--- Missed 2016-07-10 ---错过了
2016-07-11--- Missed 2016-07-11 ---错过了
2016-07-12--- Missed 2016-07-12 ---错过了
2016-07-13--- Missed 2016-07-13 ---错过了
2016-07-14--- Missed 2016-07-14 ---错过了
2016-07-15--- Missed 2016-07-15 ---错过
2016-07-16--- Missed 2016-07-16 ---错过了
2016-07-17--- Missed 2016-07-17 ---错过了
2016-07-18--- Missed 2016-07-18 ---错过了
2016-07-19--- Missed 2016-07-19 ---错过了
2016-07-20--- Missed 2016-07-20 ---错过了
2016-07-21--- Missed 2016-07-21 ---错过了
2016-07-22--- Missed 2016-07-22 ---错过
2016-07-23--- Missed 2016-07-23 ---错过了
2016-07-24--- Missed 2016-07-24 ---错过了
2016-07-25--- Missed 2016-07-25 ---错过了
2016-07-26--- Missed 2016-07-26 ---错过了
2016-07-27--- Missed 2016-07-27 ---错过了
2016-07-28--- Missed 2016-07-28 ---错过了
2016-07-29--- Missed 2016-07-29 ---错过了
2016-07-30--- Missed 2016-07-30 ---错过了
2016-07-31--- Missed 2016-07-31 ---失踪

Your question is hard to understand but as far as I understand, you're trying to check whether there are record for iterated date or not. 您的问题很难理解,但据我所知,您正在尝试检查是否有重复日期的记录。

The main point which you missed out is, you are only storing single date on $date variable. 您错过的要点是,您只在$date变量中存储单个日期。 To clarify; 澄清;

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
*/

After this code portion, your "OK" check is only valid for "2016-07-03". 在此代码部分之后,您的“确定”检查仅对“ 2016-07-03”有效。 To correct the code, you should have array to store all days and use in_array() function to check existence. 要更正代码,您应该具有存储所有天数的数组,并使用in_array()函数检查是否存在。

Another alternative is applying these procedures inside while loop after changing ORDER BY direction. 另一种选择是在更改ORDER BY方向后在while循环内应用这些过程。 If two consecutive records are not consecutive days, you will fill these gap (second level) iteration. 如果两个连续的记录不是连续的天,则将填补这些间隔(第二级)迭代。

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

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