【问题标题】:show full attendance by specifying present days and absent days in a date interval通过在日期间隔中指定当前天数和缺勤天数来显示全勤
【发布时间】:2019-07-11 08:30:10
【问题描述】:

我有每个员工的考勤日志,该员工可能只在几天内。我想在给定的日期间隔内显示当前天数和缺席天数。

例如给定的区间是:

$from = "2019-03-01";

$to = "2019-03-05";

这是数据记录:

[
 {      
   "id": 1310,
   "empid": 3,
   "check_in": "11:56",
   "check_out": "17:25",
   "date": "2019-03-02",
   "time_long": "5:28"
 }, 
 {      
   "id": 1311,
   "empid": 3,
   "check_in": "08:00",
   "check_out": "16:00",
   "date": "2019-03-04",
   "time_long": "8:00"
 },
...
]

我希望输出如下:

我会以 html 格式显示输出。

<table>
    <thead>
        <tr>
            <th>NO</th>
            <th>Date</th>
            <th>Check In</th>
            <th>Check Out</th>
            <th>Time Long</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            <td>2019-03-01</td>
            <td>X</td>
            <td>X</td>
            <td>X</td>
        </tr>
        <tr>
            <td>2</td>
            <td>2019-03-02</td>
            <td>11:56</td>
            <td>17:25</td>
            <td>5:25</td>
        </tr>
        <tr>
            <td>3</td>
            <td>2019-03-03</td>
            <td>X</td>
            <td>X</td>
            <td>X</td>
        </tr>
        <tr>
            <td>4</td>
            <td>2019-03-04</td>
            <td>08:00</td>
            <td>16:00</td>
            <td>8:00</td>
        </tr>
        <tr>
            <td>5</td>
            <td>2019-03-05</td>
            <td>X</td>
            <td>X</td>
            <td>X</td>
        </tr>
    </tbody>
</table>

【问题讨论】:

  • 您尝试了哪些方法,结果如何?

标签: php html arrays


【解决方案1】:

让我们假设 $input 具有您指定格式的数据。

这是 Json 格式,所以首先你需要对其进行解码。

接下来,您需要重新格式化数组,以便键是日期。

接下来您需要将日期更改为 DateTime 对象。

现在您可以使用 for 循环遍历这些日期。在 for 循环中,您需要检查给定日期的键是否存在于重新格式化的数组中。如果是,则使用它来填充。否则打印 X。

下面是整个代码

$results = json_decode($input);

$formatResults = array();
foreach( $results as $result) {
    $date = $result->date;
    $formatResults[$date] = $result;
}

$from = "2019-03-01";
$to = "2019-03-05";

$begin = new DateTime( $from );
$end   = new DateTime( $to );
?>
<table>
    <thead>
        <tr>
            <th>NO</th>
            <th>Date</th>
            <th>Check In</th>
            <th>Check Out</th>
            <th>Time Long</th>
        </tr> 
    </thead>
    <tbody>
<?php    
    $loopCount = 0;
    for($i = $begin; $i <= $end; $i->modify('+1 day')){
        $loopCount++;
        echo '<tr>';
            echo '<td>'. $loopCount . '</td>';
            echo '<td>'. $i->format("Y-m-d") . '</td>';
            if( isset ( $formatResults[ $i->format("Y-m-d") ] ) ) {
                echo '<td>'. $formatResults[ $i->format("Y-m-d") ]->check_in . '</td>';
                echo '<td>'. $formatResults[ $i->format("Y-m-d") ]->check_out . '</td>';
                echo '<td>'. $formatResults[ $i->format("Y-m-d") ]->time_long . '</td>';
            } else {
                echo '<td>X</td>';
                echo '<td>X</td>';
                echo '<td>X</td>';
            }
        echo '</tr>';
    }
?>
    </tbody>
</table>    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多