php - 检查日期是否在每个用户的日期范围内
问题描述
我正在尝试将用户的缺勤日期分配给他们的日历日期。
我将所有数据作为 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;
为前几名员工获得正确的价值。之后日期似乎被分配给随机日历值。
解决方案
推荐阅读
- angular - Angular 11 - 基于角色的身份验证不与其他角色一起使用(只有“操作员”在工作)
- html - html选择标签中的rails link_to
- python - 如何在 Python 中使用浮点数进行数学运算而不以 10+ 小数点结尾?
- qt - 使用 cpack 和 windeployqt 创建安装程序
- python - PySimpleGui 在弹出窗口中显示图像
- javascript - 当我在 discord.js v13 中使用它时,为什么我的别名不起作用?
- amazon-web-services - 通过 AWS CloudFront 的自定义子域在 S3 网站上通过 https 故障,但通过 HTTP 工作正常
- microsoft-graph-api - 撤销 Microsoft 聊天机器人的管理员同意
- python - 等待python中的触发器
- python - 如何使项目的顺序在两个单独的 QListWidgets 中匹配?