0

I am working on a php mysql project that need a booking app, I have however ran into a challenge of pulling out all booked time slots and only displaying available time around this.

So basically if someone booked from 09:15:00 to 09:30:00, it should show time available from 08:00:00 (date start time) to 09:15:00 (booking start time), then again next time slot from 09:30:00 to 18:00:00 (date end time).

function createForAvailableHrs($FirstStartTime, $lastEndTime, $date)
{
    $connector      = new DbConnector();

    $this_date = date("Y-m-d", strtotime($date[1]));
    $result = $connector->query("SELECT * FROM GPC_appointments WHERE deletedBy = ? AND appointmentDate = ?", array(0,$this_date));
    $rowCount = $connector->num_results($result);

    if($rowCount > 0) {

        $count = $connector->num_results($result);
        $c = 0;
        $show = '';
        $newStartTime = '';
        $lastNewEndTime = '';

        while ($row = $connector->fetchArray($result))
        { 

                //Set New check time points

                if ($row['appointmentTimeFrom'] !== '') {
                    //IF BOOKING IS LESS THAN START TIME
                    $endTime = $row['appointmentTimeTo'];
                    $startTime = $row['appointmentTimeFrom'];

                    if($newStartTime == '') { $newStartTime = $FirstStartTime; }

                    //IF BOOKING IS LESS THAN START TIME
                    if ($this->dateDiffInterval($endTime, $startTime, 'H') < 2) { $endTime = $this->AdjustEndTime($startTime); }

                    //IF BOOKING END TIME IS GREATER THAN 2 HOURS
                    if (abs($this->dateDiffInterval($endTime, $startTime, 'H')) >= 2) { $startTime = $this->AdjustEndTime($startTime); }
                     if($this->getNextEndTime($startTime,$date) !== FALSE && $this->getNextEndTime($startTime,$date) !== null) { //next startTime is > "15 min", newEndTime = nextStartTime
                        //$newEndTime = $endTime;// + strtotime($this->getNextEndTime($startTime,$date));
                        $newEndTime = $this->AdjustEndTime($endTime, $this->getNextEndTime($startTime,$date));
                     }
                     else {
                        $newEndTime = $this->AdjustEndTime($endTime, '');
                     }

                     if ($endTime > $FirstStartTime) {
                         $newEndTime = $startTime;//date("H:i:s",strtotime("+ 15 minutes",$startTime))$endTime;
                         $endTime = $FirstStartTime;
                     }

                    if (date("H:i:s", strtotime($newStartTime)) > date("H:i:s", strtotime($FirstStartTime))) { $startTime = $FirstStartTime; }

                    $show .= "<div class='bookingSlots' data-bookingFromHours='".$endTime."' "
                            . "data-timeDiff='".abs($this->dateDiffInterval($endTime, $newEndTime, 'H'))."' "
                            . "data-bookingToHours='".$newEndTime."' data-bookdate='$date[1]'>".
                            $endTime. " am to ".$newEndTime."  </div>";
                }

            $c++;
            if ($c == $count) {
                $show .= $this->createForStandardWorkHrs($date, $newEndTime);
            }
        }

        return $show;
    }
    else {
        return FALSE;
    }

}

Currently it all does not work as intended , Any help?

Geraldo Isaaks
  • 156
  • 1
  • 1
  • 20
  • If it was me, I'd forget about the PHP for now , and instead focus on the MySQL – Strawberry Apr 14 '18 at 11:50
  • 1
    Is this question similar to yours? maybe interesting? [How to push the next date/time to array which fits around current date/times in array?](https://stackoverflow.com/questions/40505794/how-to-push-the-next-date-time-to-array-which-fits-around-current-date-times-in/40551882#40551882). – Ryan Vincent Apr 14 '18 at 16:18

0 Answers0