简体   繁体   English

动态获取php数组中缺失的日期值

[英]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.我找到了一种方法,通过检查 2 个数组值之间的天差,然后计算并显示天数。 However, I am not sure how to do this dynamically between the 2 array values.但是,我不确定如何在 2 个数组值之间动态执行此操作。

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.现在您可以遍历$all_by_date ,检查条目是否为null并采取相应措施。

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

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