检查时间范围重叠,引发错误,如果属实 [英] Check if time ranges overlap and trigger error if true

查看:133
本文介绍了检查时间范围重叠,引发错误,如果属实的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,这个问题的长度,但有一些背景,你需要知道的充分理解:

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

我建立在Drupal 7事件调度程序,允许用户选中一个复选框,以将事件添加到他们的日程安排......这是一个烂摊子。该复选框包含外观类似的值 1404915600 - 1404917400 从而重新presents 上午10:30 - 10:50 这是一个使用PHP的的strtotime()的结果; 像功能,使的strtotime(10:30);

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");

我需要检查对飞如果用户检查另一个框的重叠时间为previously选择的事件,如果有重叠引发错误。

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.

目前,我使用上面提到的复选框来创造价值的PHP函数,然后这些值传递到JavaScript数组的复选框点击。在 ID 的复选框是选中的事件,它作为数组键的URL友好名称和值在选择的时候,所以阵列最终看起来像

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 }

每个事件都有它自己的键,以简化删除(或unscheduling)使用的Javascript事件删除功能。

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

下面是完整的code这抓起复选框ID和阵列中关联的时间,并将它们存储:

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];
    }        
});

我似乎无法获得新选择的事件时间成与数组中的当前时间检查。他们只是附加到 timeArr 阵列如果检查通过。

我要对这个完全错误的方式或者我只是失去了一些东西少?我试过很多不同的东西,我完全迷失了方向,以这是采取正确的方向。我使用的Drupal 7,是否可以帮助任何。

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&LT; END2&功放;&安培; END1&GT; START2 。这将需要(下)所有4个比较检测重叠。您还需要决定是否确切的开始和结束时间是包容还是排斥的。 I.E.如果事情在1000结束,别的东西在1000开始的,他们重叠的,准确的1000时间吗?

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?

重叠的四个条件是:


  1. 事件1重叠EVENT2的开始(事件1:10 am-12am,EVENT2:11 am-1pm)

  2. 事件1重叠EVENT2结束时(事件1:10 am-12am,EVENT2:9 am-11am)

  3. 事件1完全包围EVENT2(事件1:10 am-12am,EVENT2:1030am-1130AM)

  4. EVENT2完全包围事件1(事件1:10 am-12am,EVENT2:9 am-1pm)

上面的公式涵盖了所有4个可能的方案。

The formula above covers all 4 possible scenarios.

这篇关于检查时间范围重叠,引发错误,如果属实的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆