Home > Back-end >  How to show Reservation Available time gap between 8am morning to next morning 4am in php?
How to show Reservation Available time gap between 8am morning to next morning 4am in php?

Time:10-10

Previously already have customers reserve times :

09-11
12-14
23-01

$shopOpenTime = 08;
$shopCloseTime = 04;

What is the easiest way using php to list out all available times?

01-04 / 08-09 / 11-12 / 14-23

Coding :

foreach($manyOldBook as $eachOldBook) 
{
    $availableTime = $availableTime . $eachOldBook['book_end'] . " - " . $eachOldBook['book_start'] . " / ";
}

echo $availableTime;

I tried :

  1. using if else to convert 24 to 00, 25 to 01, 26 to 02...
  2. using if else to check if $previous_book_end < 04, then show $shopCloseTime.
  3. using if else to check if $previous_book_start > 08, then show $shopOpenTime.

End up there are more than 100 lines of if else statement, too messy code, too much code... I am looking for easier way to do it?

Maybe use php fmode()? w3cschool fmode tuturial

Or Maybe store all booked times in php array, and print value which is not exist in the array?

Any other creative and easier way to do it?

CodePudding user response:

I would use PHPs DateTime object for this. It will give you more flexibility, e.g. if your reservation is not a whole hour or things like that.

First convert your input data into DateTime object and the get the free blocks between.

Example:

<?php
$reservedTimes = [
    ['start' => 9,
    'end' => 11,
    ],
    ['start' => 12,
    'end' => 14,
    ],
    ['start' => 23,
    'end' => 01,
    ],
];

$shopOpenTime = new DateTime();
$shopOpenTime->setTime(8,0);

$shopCloseTime = new DateTime();
$shopCloseTime->add(new DateInterval('P1D'))->setTime(4,0);

$freeTimes = [];

//start of the free time block is the shop open time
$freeTimeStart = $shopOpenTime;

foreach ($reservedTimes as $reservedTime) {
    //add 24h if end is lower than start
    if ($reservedTime['end'] < $reservedTime['start']) {
        $reservedTime['end'] = $reservedTime['end']   24;
    }

    //end of the free time block is start of the reservation
    $freeTimeEnd = new DateTime();
    $freeTimeEnd->setTime($reservedTime['start'], 0);

    //add new element to array if start and end is not the same time
    if ($freeTimeStart != $freeTimeEnd) {
        $freeTimes[] = ['start' => clone($freeTimeStart), 'end' => $freeTimeEnd];
    }

    //start of the next free block is the end of this reservation
    $freeTimeStart->setTime($reservedTime['end'], 0);
}

//add the last element until shop closing time
$freeTimeEnd = $shopCloseTime;
if ($freeTimeStart != $freeTimeEnd) {
    $freeTimes[] = ['start' => clone($freeTimeStart), 'end' => $freeTimeEnd];
}

foreach ($freeTimes as $freeTime) {
    print('Start ' . $freeTime['start']->format('Y-m-d H:i:s') . ' ');
    print('End ' . $freeTime['end']->format('Y-m-d H:i:s'));
    print(PHP_EOL);
}

This gives you:

Start 2021-10-09 08:00:00 End 2021-10-09 09:00:00
Start 2021-10-09 11:00:00 End 2021-10-09 12:00:00
Start 2021-10-09 14:00:00 End 2021-10-09 23:00:00
Start 2021-10-10 01:00:00 End 2021-10-10 04:00:00
  • Related