php - 如果日期早于日期则显示错误

标签 php mysql sql date

目前我正在做最后一年的项目,开发酒店预订系统。现在,我陷入困境,如果输入的日期早于输入日期,以及在今天的日期之前预订,如何显示消息?

我在 mysql 数据库中记录了 dor(预订日期)、dco(退房日期)和入住时间。住宿时长根据预订日期和退房日期使用 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' ;
    ?>

有人知道如果在预订日期之前输入日期并且在今天之前预订则显示错误消息的代码吗?

最佳答案

您可以使用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()设置适当的时区。

更新:

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

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

关于php - 如果日期早于日期则显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23589549/

相关文章:

sql - ISNULL 在 SQL Server 2008 中给出错误的值

sql - 如何将多列制表符分隔的文本文件导入单列 PostgreSQL 表?

sql - 如何在 PostgreSQL 中批量插入新行

Javascript 如果函数不能与 getElementById 一起正常工作

php - 在 PHP 中获取下一个/上一个 ISO 周和年份

php - 有没有比这种方式更好的方法在 MySQL 中执行 SELECT 查询并在 PHP 中对它们进行排序?

python - 我如何加速(或分解)这个 MySQL 查询?

php - 禁用 JavaScript 时模拟点击功能

MySQL 缓存一切

尝试更改表时 Python MySQLdb 错误