douxin2002 2014-07-09 19:57
浏览 75
已采纳

检查时间范围是否重叠,如果为真则触发错误

Sorry for the length of this question, but there is some background you need to know to fully understand:

I am building an event scheduler in Drupal 7 which allows users to check a checkbox to add an event to their schedule... and it's a mess. The checkboxes contain a value looking similar to 1404915600 - 1404917400 which represents 10:30am - 10:50am which is the result of using PHPs strtotime(); function like so strtotime("10:30am");

I need to check on the fly if the user checks another box with an overlapping time as a previously selected event and trigger an error if there is an overlap.

Currently, I am using the php function mentioned above to create the values in the checkboxes, and then passing those values to a javascript array on click of checkbox. The id of the checkbox is the url friendly name of the selected event, which acts as the array key, and the values are the selected times, so the array ends up looking like:

timeArr = { event-name-one: 0: 1404915600, 1: 1404917400 }

Each event has it's own key to simplify deleting (or unscheduling) events using Javascripts delete function.

Here is the complete code which grabs the checkbox ID and the times associated and stores them in the array:

var timeBox = jQuery('input[type="checkbox"].time, input[type="checkbox"].time2');
var timeArr = [];

// Listen for checkbox change
timeBox.bind('click', function() {

    var eventID = jQuery(this).attr('id');

    // If Checkbox IS checked
    if(jQuery(this).prop('checked')) {

        var timeStamp = jQuery(this).val();
        var newTime = timeStamp.split(" - ");

        // If timeArr has been set
        if(timeArr.length > 1) {

            for(key in timeArr) {
                if(newTime[0] > timeArr[key][0] && newTime[0] < timeArr[key][1] || timeArr[key][0] > newTime[0] && timeArr[key][0] < newTime[1]) {
                    alert("Overlap!");
                    return false;
                } else {
                    timeArr[eventID].push(newTime);
                    console.log(timeArr);
                }
            }           

        } else {
            // else timeArr hasn't been set -- so set it!                
            timeArr[eventID] = newTime;
            console.log(timeArr);
        }
    } else {
        delete timeArr[eventID];
    }        
});

I can't seem to get the newly selected event time to cross check with the current times in the array. They just get appended to the timeArr array as if the checks passed.

Am I going about this the complete wrong way or am I just missing something little? I've tried so many different things I'm quite lost as to which is the right direction to take. I am using Drupal 7 if that helps any.

  • 写回答

1条回答 默认 最新

  • doucan8049 2014-07-09 20:57
    关注

    The formula is start1 < end2 && end1 > start2. This will detect overlap in all 4 comparisons needed (below). You also need to decide if the exact start and end time is inclusive or exclusive. I.E. if something ends at 1000 and something else starts at 1000 are they overlapping for that exact 1000 minute?

    The four conditions of overlap are:

    1. Event1 overlaps the start of Event2 (Event1: 10am-12am, Event2: 11am-1pm)
    2. Event1 overlaps the end of Event2 (Event1: 10am-12am, Event2: 9am-11am)
    3. Event1 completely encompasses Event2 (Event1: 10am-12am, Event2: 1030am-1130am)
    4. Event2 completely encompasses Event1 (Event1: 10am-12am, Event2: 9am-1pm)

    The formula above covers all 4 possible scenarios.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据