doubomudichen0832 2019-06-11 09:38
浏览 126

如何查看日期的同一天的时间段重叠?

I have shifts created for different user groups. The shift includes shift start-time and end-time, days of the week(multi-select) and date period(from the date - to date) applicable.

Where shift select for a particular user is multiple select as one user can have multiple shifts on the same day or different day on different time. So how do I check that selected shifts do not overlap the way that one user can not have two shifts at the same time?

I have tried checking first, weekdays intersect or not, if it intersects then check the date and time period overlaps or not using the code below but did not get a correct result.

/**
 * @Route("/shift/checkTimeConflict", methods={"POST"})
 */
public function checkTimeConflict(Request $request)
{
    $data = $request->request->all();
    if (isset($data['shifts']) && count($data['shifts']) > 1) {
        $shifts = $this->getDoctrine()
            ->getRepository(Shift::class)
            ->findById($data['shifts']);
        foreach ($shifts as $key => $val) {
            $shift_data[$key]['id'] = $val->getId();
            $shift_data[$key]['title'] = $val->getTitle();
            $shift_data[$key]['weekday'] = $val->getWeekday();
            $shift_data[$key]['start_time'] = $val->getStartTime()->format('H:i');
            $shift_data[$key]['end_time'] = $val->getEndTime()->format('H:i');
            $shift_data[$key]['time_period_start'] = $val->getTimePeriodStart()->format('Y-m-d');
            $shift_data[$key]['time_period_end'] = $val->getTimePeriodEnd()->format('Y-m-d');
        }

        for ($i=0; $i < count($shift_data); $i++) { 
            if ($this->checkDateTimeOverlaps($shift_data[$i], $shift_data))
                return new JsonResponse(true);
        }
    }

    return new JsonResponse(false);
}

/**
 * Check provided shift time for the day and period overlaps with other shifts
 */
public function checkDateTimeOverlaps($shift, $shift_data)
{
    $start = strtotime($shift['time_period_start'].' '.$shift['start_time']);
    $end = strtotime($shift['time_period_end'].' '.$shift['end_time']);
    for ($i=0; $i < count($shift_data); $i++) {
        if ($shift['id'] != $shift_data[$i]['id']) {
            $check_start = strtotime($shift['time_period_start'].' '.$shift['start_time']);
            $check_end =  strtotime($shift['time_period_end'].' '.$shift['end_time']);
            if (!empty(array_intersect($shift['weekday'], $shift_data[$i]['weekday']))) {
                if (($start > $check_start && $start < $check_end) || ($end > $check_start && $end < $check_end) || ($check_start > $start && $check_start < $end) || ($check_end > $start && $check_end < $end)) {
                    return true; // intersects
                }
            }
        }
    }
    return false; // no conflict
}

For example, selected shifts have the below data:

(1) 09:00-13:00, Monday-Friday, 2019-01-01 - 2020-01-01, First-Half

(2) 14:00-18:00, Monday-Friday, 2019-01-01 - 2020-01-01, Second-Half

(3) 18:00-20:00, Monday-Friday, 2019-01-01 - 2020-01-01, Over-Time

(4) 09:00-14:00, Saturday, 2019-01-01 - 2020-01-01, Saturday Half Day

(5) 09:00-13:00, Monday-Friday, 2020-02-01 - 2021-02-28, Part-Time

The above should return no conflict [checkDateTimeOverlaps() should return false], as there is no intersection of time for the same day and date period. However, if I replace the 5th row as below:

(5) 09:00-13:00, Monday-Friday, 2019-01-01 - 2019-01-31, Part-Time

it should return conflict [checkDateTimeOverlaps() should return true], as one person can not have two shifts at the same time. In the above case, row 1 and row 5 have an intersection.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥200 uniapp长期运行卡死问题解决
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
    • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
    • ¥15 乘性高斯噪声在深度学习网络中的应用
    • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决