简体   繁体   中英

How to check booked slots with dates given

This is my array and I want to return Array who has already booked date-wise.
Currently it is working time-wise, but when I do this for more than one day it stops working:

$bookedData  = array(   
    Id : 1
    date : "2020-03-31"
    endDate : "2020-04-01"
    time : "19:00:00"
    endTime : "11:30:00"
)
foreach($bookedData as $booked) {             /* checking fr all slots*/
    if (strlen($booked->time) > 0 && strlen($booked->endTime) > 0 {
        if ($booked->time !=$booked->endTime) {
            $appStrtTime = strtotime($booked->time); /* It gives time startTime */
            $appEndTime = strtotime($booked->endtime); /* It gives endTime */

            $AddMins = 5*60;
            while ($appStartTime < $appEndTime) {  /* if starttime is less than endtime then return false */
                $time = date("G:i:s", $appStartTime);
                $key = array_search($time, $ReturnArray);
                if($key!= false) {
                    unset($ReturnArray[$key]); /* if slot already available then unset it */
                }
                $appStartTime +=$AddMins;
            }
            $appStartTime += $AddMins;
        }
    } else {
        $key = array_search($booked->time, $ReturnArray);
        if ($key != false) {
                unset($ReturnArray[$key]);
        }
    }
    }
}
if (count($ReturnArray) > 0) {
    unset($ReturnArray[count($ReturnArray) - 1]);
}
$ReturnArray = array_values($ReturnArray);  
}

Here are some edits of your code:

$appStartTime = strtotime($booked['date'] . ' ' . $booked['time']);
$appEndTime = strtotime($booked['endDate'] . ' ' . $booked['endTime']);
while ($appStartTime < $appEndTime) 
{

   $time = strtotime($appStartTime);
   // ...carry on with the rest of the code

}

According to your comment

For example if i select starttime date as 31 march and time as 19:00 then i want to get all the slots after that time like 19:00,19:15,... Upto end date

I would go for this code here:

$start = new \DateTime('2020-03-31 19:00');
$end = new \DateTime('2020-03-31 21:30');
$slotSize = 15; // 15 minute intervals

$slots = [];
do {
    // Add here your custom code to skip specific timeslots
    // As an example I used a custom function as an example
    if (slot_is_blocked_by_existing_bookings($slot, $bookings)) {
        continue;
    }

    $slots[] = clone $start;
} while ($start->add(new \DateInterval("PT{$slotSize}M")) <= $end);

and here is an example output of the $slots -list which contains DateTime-objets (string formatted to RFC3339 for readability reasons)

array:11 [
  0 => "2020-03-31T19:00:00+00:00"
  1 => "2020-03-31T19:15:00+00:00"
  2 => "2020-03-31T19:30:00+00:00"
  3 => "2020-03-31T19:45:00+00:00"
  4 => "2020-03-31T20:00:00+00:00"
  5 => "2020-03-31T20:15:00+00:00"
  6 => "2020-03-31T20:30:00+00:00"
  7 => "2020-03-31T20:45:00+00:00"
  8 => "2020-03-31T21:00:00+00:00"
  9 => "2020-03-31T21:15:00+00:00"
  10 => "2020-03-31T21:30:00+00:00"
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM