如果日期在日期之前显示错误

时间:2014-05-11 06:11:51

标签: php mysql sql date

目前,我正在开发酒店预订系统的最后一年项目。如果在日期之前输入日期,以及在今天的日期之前预订,我现在仍然坚持如何显示消息?

我有dor(预约日期),dco(日期签出)以及在mysql数据库中停留的时间。根据日期预约和退出日期,使用DATEDIFF()计算一天的停留时间

下面的

是我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' ;
    ?>

如果在今天之前预订和预订日期之前输入日期,是否有人知道显示错误消息的代码?

1 个答案:

答案 0 :(得分:1)

您可以使用strtotime()并执行以下操作:

$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";
}

首先确认预订日期$dateIn不在当前日期之前。然后确认结帐日期$dateOut不在预订日期之前$dateIn

注意,根据php.ini的配置方式,您可能需要使用date_default_timezone_set()来设置相应的时区。

更新:

要在您的程序中实现此功能,请使用当前的样式和设置,就在此块之后:

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

添加:

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();
}