0

I'm working with fullcalendar and unable to see a solution for my problem. I've tried many things. Stackoverflow posts and my own ideas (based on SO posts).

The problem/challenge

When I add a event on a fullcalendar. It adds it to the calendar and thats what I want. But there should be a restriction on overlapping events. So that there would be no overlapping events.

My question is extremly similar to Determining if two time ranges overlap at any point

What I have

A list of events in a array which I fetch from the database.

The database

enter image description here

I've the start and endtime from the event I drop in any format available (e.g. timestamp/date)

Example

enter image description here

Finially the code

Efficient way to check if current date is between 2 dates (years dont matter) based on the accepted answer here

if($error == false){
    foreach($events as $sKey=>$dbEvent){

        if(strtotime($dbEvent['start_time']) == strtotime($event->startTime))
        {
            $sKey = 0;
            break;
        }
        if((strtotime($dbEvent['start_time']) < strtotime($event->startTime)) && (strtotime($dbEvent['end_time']) > strtotime($event->startTime))){
            break;
        }else{
            $sKey = null;
        }
    }

    foreach($events as $eKey=>$dbEvent){

        if(strtotime($dbEvent['end_time']) == strtotime($event->endTime))
        {
            $eKey = 0;
            break;
        }
        if((strtotime($dbEvent['end_time']) < strtotime($event->endTime)) && (strtotime($dbEvent['start_time']) > strtotime($event->endTime))){
            break;
        }else{
            $eKey = null;
        }
    }


    //Checking if there is some event overlapping
    if($sKey ==null && $sKey != '0'){
        $error = false;
    }else{
        $error = true;
        $errorMessage = 'bezet';
    }

    if($eKey ==null && $eKey != '0'){
        $error = false;
    }else{
        $error = true;
        $errorMessage = 'bezet';
    }
}

There are no problems with executing the code. It just doesn't give the errors when I do expect them. I'm 100% sure there is a lot of code that could written shorter. I refactor after this problem works.

My brain is stuck at this problem because if the start is not overlapping and the end isn't overlapping like in the first scenario. I've tried many solutions maybe I'm just missing a point.

What does work When the start and end time are exact the same as the event that is posted.

Any help tips / hints / comments would be appreciated.

Using fullcalendar fork:

https://github.com/seankenny/fullcalendar

https://github.com/arshaw/fullcalendar

After rereading the linked stackoverflow questions... I changed my code to this:

foreach($events as $dbEvent){
        $dbEventStart = strtotime($dbEvent['start_time']);
        $dbEventEnd   = strtotime($dbEvent['end_time']);
        $evStart      = strtotime($event->startTime);
        $evEnd        = strtotime($event->endTime);

        if (($evStart < $dbEventEnd) && ($evEnd > $dbEventStart)) {
            $error = true;
            $errorMessage = 'het lijkt te werken';  
        }
    }

The evStart / evEnd (are the newly created events) and the dbevent are the existing events.

This magic fixed my problem :)

Community
  • 1
  • 1
JochemQuery
  • 1,495
  • 2
  • 26
  • 38
  • possible duplicate of [Determining if two time ranges overlap at any point](http://stackoverflow.com/questions/13387490/determining-if-two-time-ranges-overlap-at-any-point) – JochemQuery Apr 14 '14 at 14:29
  • 1
    maybe this can help you http://stackoverflow.com/questions/15800398/can-i-prevent-events-with-conflict-time/15804896#15804896 – nikos.svnk Apr 14 '14 at 14:34
  • @nikos.svnk thank you, I will use that for client side validation :) – JochemQuery Apr 14 '14 at 14:41

0 Answers0