首页 > 解决方案 > 检查日期是否在每个用户的日期范围内

问题描述

我正在尝试将用户的缺勤日期分配给他们的日历日期。

我将所有数据作为 Laravel 中的集合获取。

用户的日历看起来像这样,它只是日期:

"cal_entries": [ { "CAL_Date": "2021-01-01 00:00:00.000", }, { "CAL_Date": "2021-01-02 00:00:00.000", }, { "CAL_Date": "2021-01-03 00:00:00.000", }, { "CAL_Date": "2021-01-04 00:00:00.000", }, { "CAL_Date": "2021-01-05 00:00: 00.000", }, ...等等,直到月底。

用户的缺勤数据如下所示:

“缺席”:[{“ABS_Start”:“2021-01-04 00:00:00.000”,“ABS_End”:“2021-01-05 00:00:00.000”,},{“ABS_Start”:“2021- 01-07 00:00:00.000”,“ABS_End”:“2021-01-10 00:00:00.000”,}]

所以基本上我只是检查日历中的日期是否等于或早于 ABS_Start 并且等于或小于 ABS_End。简单的。

这是我使用的函数的一部分:

// Each employee has its own calendar and absences arrays
        foreach ($employees as $employee) {

            $calendarEntries = null;
            $absences = null;

            // Here I'm getting two arrays, calendar and absences
            $calendarEntries = $employee->cdnProfile->calendarEntries;
            $absences = $employee->cdnProfile->absences;

            // Employee may not have any absences
            if ($absences->isNotEmpty()) {

                // Get each date from calendar i.e. 2021-01-01 00:00:00.000
                foreach ($calendarEntries as $calendarEntry) {

                    $absence = $absences->where('ABS_Start', '<=', $calendarEntry->CAL_Date)
                        ->where('ABS_End', '>=', $calendarEntry->CAL_Date)->first();

                    // If match the create 'test' object and add that absence to that calendar entry
                    if ($absence) {
                        $calendarEntry->test = $absence;
                    }
                }
            }
        }

        return $employees;

我遇到的问题是$calendarEntry->test = $absence;为前几名员工获得正确的价值。之后日期似乎被分配给随机日历值。

标签: phpmysqllaravel

解决方案


推荐阅读