mardi 5 janvier 2016

Check for overlapping time ranges is not working

I wonder if you can help me, I'm trying to check whether a time range overlaps with another, I found an answer here and I implemented on code. At first it seems to be working, but I tried a bunch of cases to see if it doesn´t fail at some point but it does.

This is my code

public function consult(BookingRequest $request){
    // Get all the records from my db that match with day and room requested
    $mbs = Booking::where('room_id','=',$request->room)->where('day','=',$request->day)->get();
    // Range time which is consulted
    $startB = strtotime($request->day.$request->start);
    $endB = strtotime($request->day.$request->end);
    $isTrue = false;
    //
    foreach($mbs as $mb){
        // range time for each Booking record in the db
        $startA = strtotime($mb->day->format('Y-m-d').$mb->start);
        $endA = strtotime($mb->day->format('Y-m-d').$mb->end);
        // Check for overlap
        if(($startB < $endA) && ($endB > $startA))
            dd('overlap id='.$mb->id);// $istrue = true
        else
            dd('no overlap id='.$mb->id);// $isTrue = false
    }
    //dd($result);

    // If no overlapping, then book
    if(!$isTrue){
        $book = $request;
        // Get all the bookings on day requested
        $bookings = Booking::where('day','=',$book->day)
                            ->where('room_id','=',$book->room)->get();
        // creating events for the calendar
        $events = [];
        foreach($bookings as $booking){
            $events[] = \Calendar::event(
                ''.$booking->room->name, //event title
                false, //full day event?
                $booking->day->format('Y-m-d').$booking->start, //start time (you can also use Carbon instead of DateTime)
                $booking->day->format('Y-m-d').$booking->end, //end time (you can also use Carbon instead of DateTime)
                $booking->id //optionally, you can specify an event ID
            );
        }
        $events [] = \Calendar::event(
            ''.Room::find($book->room)->name, //event title
            false,
            $book->day.$book->start,
            $book->day.$book->end,
            0,
            [
                'backgroundColor' => '#ff5722'
            ]
        );
        // Adding event for the calendar
        $calendar = \Calendar::addEvents($events)->setOptions([ 'defaultDate' => $book->day,'defaultView' => 'agendaDay','lang' => 'es']);
        return view('consult.book')->with(['calendar' => $calendar,'book'=>$book]);
    }
    // If a record matches then redirect back
    else{
        Session::flash('flash_message','Lo sentimos ese horario está ocupado');
        return redirect()->back();
    }

}

So I check for 01/08/2016 and I have two records on that date

enter image description here

Then If I consult for
2016-01-08T0800 to 2016-01-08T0930
2016-01-08T0800 to 2016-01-08T1100, ... and so on, it works (what I get is overlap)
and when I consult for
2016-01-08T0930 to 2016-01-08T1530
2016-01-08T1230 to 2016-01-08T1530
2016-01-08T1100 to 2016-01-08T1400
2016-01-08T0930 to 2016-01-08T1530, it doesn't work (What I get is no overlap), but the right answer is overlap according to my schedule above.

I would appreciate some help !!!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire