StuBee
StuBee

Reputation: 89

Preventing double booking and booking before today php pdo

I have a booking form set up where the user can book different facilities but I can't get my head around how to prevent the user from booking a date that is before today or how to prevent them from double booking.

I did try and work with a datepicker but I am using the HTML5 standard datepicker and also using Twitter Bootstrap but I don't know how to restrict dates from the user.

Below includes my full form and the PHP included in it.

<?php
        include "config.php"; 

        //Booking point
        if(isset($_POST['booking']))
        {
            //get values for variables
            $pitchID = $_POST['pitchID'];
            $start_date = $_POST['start_date'];
            $start_hour = $_POST['start_hour'];
            $end_hour = $_POST['end_hour'];
            $booking_age = $_POST['booking_age'];
            $pitch_size = $_POST['pitch_size'];
            $light_tokens = $_POST['light_tokens'];

            $q = $db->prepare("INSERT INTO booking SET pitchID = ?, start_date = ?, start_hour = ?, end_hour = ?, booking_age = ?, pitch_size = ?, light_tokens = ?");
            $query = $q->execute(array($pitchID,$start_date,$start_hour,$end_hour,$booking_age,$pitch_size,$light_tokens));

            $count = $q->rowCount();
                if($count == 0)
                {
                    echo "Your booking has been made";
                    header("Location:home2_template.html");
                    return; 
                }else {
                    echo "That booking already exists";
                }
        }

    ?>

Upvotes: 1

Views: 454

Answers (1)

Jonas R.
Jonas R.

Reputation: 28

To check if a date is before a certain date you could convert your dates to timestamp and check if one is lower than the other

$start_date = $_POST['start_date'];

if( strtotime($start_date) < time()) {
    // Date is before today
} else {
    // Date is after today
}

Upvotes: 1

Related Questions