简体   繁体   中英

Get missing date values from array in php dynamically

We are creating a system where employees need to update their daily status report. The fundamental purpose of the system is to note the missing dates on which they did not update the status report.

I have found a way to do that by checking the day difference between the 2 array values and then counting & displaying the days. However, I am not sure how to do this dynamically between the 2 array values.

Here's the code I have used along with the output:

//id of the person updating the DSR
                    $userid = $_id;

                    // Array to fetch all the DSR by specific user
                    $fine = getDSRbyUserIdOrderDate($userid);

                    $today = date('d-m-Y');
                    $tomorrow = date("d-m-Y", strtotime($today . " +1 day"));

                    $ok = count($fine) - 1;

                    //Array to get dates
                    $d1 = $fine[0]['date'];
                    $d2 = $fine[1]['date'];
                    $d3 = $fine[2]['date'];

                    // Function call to find date difference 
                    $dateDiff = dateDiffInDays($d1, $d2); 
                    $dateDiff1 = dateDiffInDays($d2, $d3); 

                    echo "<h4 style='color:red'>You have missed the DSR on the following dates.</h4>";

                    for($p = 1; $p < $dateDiff; $p++){
                          $missingdate = date("d-m-Y", strtotime($d1 . " +$p day"));    
                          echo "<span style='color:red'>$missingdate</span>";
                          echo "<br />";   
                    }  

                    for($p = 1; $p < $dateDiff1; $p++){
                          $missingdate = date("d-m-Y", strtotime($d2 . " +$p day"));    
                          echo "<span style='color:red'>$missingdate</span>";
                          echo "<br />";   
                    }  


                    if($d2 != $today){
                        echo "<span style='color:red'>$today <i>- Kindly update DSR before midnight</i></span> "; 
                        echo "<br />";
                    }

Output: 在此处输入图片说明

I would create first a list of entries by date and then "paint" it accordingly.

$starting_date = new DateTime('2019-11-23');
$num_days = 10

$from_db_by_date = [];
foreach ($fine as $entry) {
  $from_db_by_date[$entry['date']] = $entry;
}

$all_by_date = [];
for ($i = 0; $i < $num_days; $i++) {
  $date_str = $starting_date->format('d-m-Y');
  $all_by_date[$date_str] = $from_db_by_date[$date_str] ?: null;
  $starting_date->modify('+1 day');
}

Now you can loop through $all_by_date , check if the entry is null and act accordingly.

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