0

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: enter image description here

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hey thanks, man, the code works, however, a small error coming up. Notice: Undefined index: 16-11-2019 in D:\xampp\htdocs\catking\intranet\dsr_personal.php on line 52 Also, if you could explain this link of the code it would be a great help. $all_by_date[$date_str] = $from_db_by_date[$date_str] ?: null;
$a = $b ?: $c; means "$a equals $b if it exists and it contains a non-falsy value, $c otherwise". Regarding the dates, I had not realised that you are using d-m-Y. I change the answer accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.