Question

Currently I'm doing final year project on developing hotel reservation system. Now I'm stuck on how to show the message if date out entered before the date in, as well as booking before today's date?

I had dor (date of reservation), dco (date check-out), and length of stay in mysql database. Length of stay counts the day using DATEDIFF() based on date reservation and date out

below is my code of the reservation.php

    protect_page();
    include 'includes/overall/header.php' ; 


    //if form is being submitted
    if(empty($_POST)=== false)
    {
        //to validate whether user enters smtg or not otherwise no point continue to do the next validation
        //create an array
        $required_fields = array ('user_id','full_name','passport','dor','dco');
        foreach($_POST as $key=>$value)
        {

            //if the key (value) in array of $required_fields is true which is empty
            if(empty($value) && in_array ($key, $required_fields) === true )
            {
                $errors[] = 'You must filled up all of the fields';
                //the validation can happen to more than 1 field
                break 1;
            }
        }

        if(empty($errors) === true)
        {
            if (!preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $_POST['dor']))
            {
                echo 'Input your Date of Reservation correctly!';
                ?>
                Click <a href="reservation.php">here</a> to try again!
                <?php
                exit();
            }

            else if (!preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $_POST['dco']))
            {
                echo 'Input your Check-out date correctly!';
                ?>
                Click <a href="reservation.php">here</a> to try again!
                <?php
                exit();
            }

            else if (!preg_match('/^[1-9][0-9]{0,2}$/', $_POST['num_of_rooms'])) 
            {
                echo 'Your Number of rooms must be filled!';
                ?>
                </br>
                Click <a href="reservation.php">here</a> to try again!
                <?php
                exit();
            }
        }
    }
    //what does this line does is that to check whether success is in the end of the URL
    if(isset($_GET['success']) && empty($_GET['success']))
    {

        view_reservation();

    }
    else
    {//if there are no errors
        if (empty($_POST) === false && empty($errors) === true)
        {
            user_reservation();
        }

        //
        else if (empty($errors) === false)
        {
            echo output_errors($errors);
        }

    ?>
    <link rel="stylesheet" type="text/css" media="all" href="jsDatePick_ltr.min.css" />
    <script type="text/javascript" src="js/jsDatePick.min.1.3.js"></script>

    <script type="text/javascript">
        window.onload = function(){
            new JsDatePick({
                useMode:2,
                target:"dor",
                dateFormat:"%Y-%m-%d"

            });

            new JsDatePick({
                useMode:2,
                target:"dco",
                dateFormat:"%Y-%m-%d"

            });
        };


    </script>



    <h1>RESERVATION </h1>


    <form action="" method="post">
    <fieldset>
    <legend> 
        <font size="6">Please input your information correctly</font>
    </legend>

    <p>

    <form action="" method="post">
        <ul>
            <li>
                Full name*: <br>
                <input type="text" name="fullname">
            </li>
            <li>
                Contact No.: <br>
                <input type="text" name="contactno">
            </li>
            <li>
                IC/Passport*: <br>
                <input type="text" name="passport">
            </li>
            <li>
                Room Type*: <br>
                <select name="roomtype" id="roomtype">
                <option value="">Select</option>
                <option value="Single">Single (RM 100)</option>
                <option value="Superior">Superior (RM 200)</option>
                <option value="Deluxe">Deluxe (RM 300)</option>
              </select>
            </li>
            <li>
                Number of Rooms*:</li>
                <select name="num_of_rooms" id="num_of_rooms">
                <option value="">Select</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
              </select>
                <br>
            <li>
                Date of reservation*: <br>
                <input type="text" size="12" id="dor" name="dor"/>
            </li>
            <li>
                Check-out Date*: <br>
                <input type="text" size="12" id= "dco" name="dco"/>
            </li>

              <input type="submit" value="Submit">
            <input type="reset" value="Clear" >
            <li>
            <br>
        </ul>
    </form>

    <?php
    }
    include 'includes/overall/footer.php' ;
    ?>

Did anyone know the code of showing error message if date out entered before date of reservation and booking before today?

Was it helpful?

Solution

You can use strtotime() and do something like this:

$dateIn = $_POST['dor'];
$dateOut = $_POST['dco'];

if (strtotime($dateIn) < time()) {
    echo "Error: Check in date is before today";
}
elseif (strtotime($dateOut) < strtotime($dateIn)) {
    echo "Error: Check out date is before check in date";
}

This first confirms that the date of reservation, $dateIn, is not before the current date. Then confirms that the date of check out, $dateOut, is not before the date of reservation, $dateIn.

Note, depending on how your php.ini is configured, you may need to use date_default_timezone_set() to set the appropriate timezone.

Update:

To implement this into your program, using your current style and setup, just after this block:

else if (!preg_match('/^[1-9][0-9]{0,2}$/', $_POST['num_of_rooms'])) 
{
    .....
}

Add this:

elseif (strtotime($_POST['dor']) < time()) 
{
    echo 'Date of reservation cannot be in the past!';
    ?>
    </br>
    Click <a href="reservation.php">here</a> to try again!
    <?php
    exit();
}

elseif (strtotime($_POST['dco']) < strtotime($_POST['dor'])) 
{
    echo 'Check-out date cannot be before Date of Reservation!';
    ?>
    </br>
    Click <a href="reservation.php">here</a> to try again!
    <?php
    exit();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top