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

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

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 MATLAB动图问题
    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名
    • ¥65 汇编语言除法溢出问题