php - How to check for available places with if statement?

I??�m working on a reservation system for a camping website. There are several places where you can sit, but it shouldn't be possible to have 2 different family??�s on one place.

That??�s why I want to make an if statement to check if the given date is already booked at the places, or is available at any of the places.

If one place is booked, I want it to check for another available spot with the same time, if no spots around the same time are available it should say "There's no available spots around this time", if there does turn out to be 1 spot available, it should automatically reserve at that spot and tell the user.

What kind of if statement do I need to use?

Few notes:

Begintijd = starting time

Eindtijd = endtime

reserveringen = reservations

reserveringstijden = reservationtimes

<?php
if (!isset($_SESSION['id'])){
    header('Location: login.php');
}

$errors = [];
if(isset($_POST['submit'])) {
    if ($_POST['begintijd'] == '') {
        $errors['begintijd'] = "Wat is de eerste dag dat je op de camping wilt staan? ontbreekt";
    }
    if ($_POST['eindtijd'] == '') {
        $errors['eindtijd'] = "Wat is de laatste dag dat je op de camping wilt staan? ontbreekt";
    }
    if (count($errors) == 0) {
      
        //Hier haal je de plaatsen op 
        $sql = "SELECT * FROM reservering WHERE type = ? ";
        $stmt = $conn->prepare($sql);
        $stmt->execute([$_POST['type']]);
        $result = $stmt->fetchAll();
       
       
        //Hier haal je de reserveringstijden op
        $sql1 = "SELECT * FROM reserveringstijden";
        $stmt1 = $conn->prepare($sql1);
        $stmt1->execute();
        $result1 = $stmt1->fetchAll();
        
        $begintijd = strtotime($_POST['begintijd']);
        $eindtijd = strtotime($_POST['eindtijd']);
        
        var_dump($_POST['begintijd']);
        if (($begintijd <= $result1[0]['begintijd'])&&($begintijd < $result1[0]['eindtijd']))
        {
            echo "hallo?";
            exit;
            if (($eindtijd = $result1[0]['begintijd'])&&($begintijd < $result1[0]['eindtijd']))
            {
                echo "stap verder";
                exit;
            }
            
        }
        else
        {
            echo "mag niet hier komen";
            exit;
            /*$sql2 = "INSERT INTO `reserveringstijden`(`id_plaats`, `begintijd`, `eindtijd`, `id_kampeerder`) VALUES (?,?,?,?) ";
            $stmt2 = $conn->prepare($sql2);
            $stmt2->execute([$result[0]['id'], $_POST['begintijd'], $_POST['eindtijd'], $_SESSION['id']]); */
        }
        
        
                            
                    
       
    }
}
?>
    <p>U wilt reserveren: </p>
    <div class="blockform">
        <form method="post">
            <label for="Plaats">Waar slaap je in?</label><br>
            <select id="type" name="type"><br>
            <option value="tent">tent</option>
            <option value="camper">camper</option>
            </select>
            <label>Eerste dag</label><br>
            <input type="date" id="begintijd" name="begintijd" value=" <?php echo (isset ($_POST['begintijd']) ? $_POST['begtintijd'] : ''); ?> "><br>
            <label>Laatste dag</label><br>
            <input type="date" id="eindtijd" name="eindtijd" value=" <?php echo (isset ($_POST['eindtijd']) ? $_POST['eindtijd'] : ''); ?> "> <br>
            <button type="submit" name="submit">reserveren</button>
        </form>
    </div>

Answer

Solution:

Let me assume that you have two parameters, say $begin and $end, a reservations table that has columns begin_time and end_time, and a spaces table that has the spaces.

Then the logic for overlaps looks like this:

where $begin < r.end_time and
      $end > r.begin_time;

Two time periods overlaps if one begin before the second ends and the first ends after the second begins.

You can return the spaces that are available during a period of time using not exists:

select s.*
from spaces s
where not exists (select 1
                  from reservations r
                  where r.space_id = s.space_id and
                        $begin < r.end_time and
                        $end > r.begin_time
                 );

Source