php - Comparing start and end time with other time 'blocks' -
i'm working fullcalendar , unable see solution problem. i've tried many things. stackoverflow posts , own ideas (based on posts).
the problem/challenge
when add event on fullcalendar. adds calendar , thats want. there should restriction on overlapping events. there no overlapping events.
my question extremly similar determining if 2 time ranges overlap @ point
what have
a list of events in array fetch database.
the database
i've start , endtime event drop in format available (e.g. timestamp/date)
example
finially code
efficient way check if current date between 2 dates (years dont matter) based on accepted answer here
if($error == false){ foreach($events $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 $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 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 no problems executing code. doesn't give errors when expect them. i'm 100% sure there lot of code written shorter. refactor after problem works.
my brain stuck @ problem because if start not overlapping , end isn't overlapping in first scenario. i've tried many solutions maybe i'm missing point.
what work when start , end time exact same event posted.
any tips / hints / comments appreciated.
using fullcalendar fork:
https://github.com/seankenny/fullcalendar
https://github.com/arshaw/fullcalendar
after rereading linked stackoverflow questions... changed code this:
foreach($events $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 newly created events) , dbevent existing events.
this magic fixed problem :)
Comments
Post a Comment