Php 如果日期早于输入日期,则显示错误
目前我正在做最后一年的项目,开发酒店预订系统。现在我被困在如何显示消息,如果输入日期之前输入日期,以及在今天日期之前预订 我有Php 如果日期早于输入日期,则显示错误,php,mysql,sql,date,Php,Mysql,Sql,Date,目前我正在做最后一年的项目,开发酒店预订系统。现在我被困在如何显示消息,如果输入日期之前输入日期,以及在今天日期之前预订 我有dor(预订日期)、dco(退房日期)和在mysql数据库中的停留时间。停留时间根据预约日期和截止日期,使用DATEDIFF()统计一天 protect_page(); include 'includes/overall/header.php' ; //if form is being submitted if(empty($_POS
dor
(预订日期)、dco
(退房日期)和在mysql数据库中的停留时间。停留时间根据预约日期和截止日期,使用DATEDIFF()
统计一天
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' ;
?>
下面是我的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' ;
?>
protect_page();
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' ;
?>
包括“includes/totall/header.php”;
//如果正在提交表格
if(空($\u POST)==false)
{
//要验证用户是否输入smtg,否则无需继续进行下一次验证
//创建一个数组
$required_fields=array('user_id'、'full_name'、'passport'、'dor'、'dco');
foreach($\发布为$key=>$value)
{
//如果$required_字段数组中的键(值)为true,则为空
if(空($value)&&in_数组($key,$required_字段)==true)
{
$errors[]=“您必须填写所有字段”;
//验证可以发生在多个字段上
破口1;
}
}
if(空($errors)==true)
{
如果(!preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/,$POST['dor']))
{
echo“正确输入您的预订日期!”;
?>
单击以重试!
单击以重试!
单击以重试!
window.onload=函数(){
新JsDatePick({
使用模式:2,
目标:“多尔”,
日期格式:“%Y-%m-%d”
});
新JsDatePick({
使用模式:2,
目标:“dco”,
日期格式:“%Y-%m-%d”
});
};
预订
请正确输入您的信息
-
全名*:
-
联系电话:
-
IC/护照*:
-
房间类型*:
挑选
单人房(100 RM)
高级(200 RM)
豪华型(300 RM)
-
房间数量*:
挑选
1.
2.
3.
-
预订日期*:
-
退房日期*:
-
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' ;
?>
有人知道如果在预订日期之前输入date out并在今天之前预订,则显示错误消息的代码吗?您可以使用并执行以下操作:
$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";
}
添加以下内容:
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();
}
elseif(strotime($\u POST['dor'])
{
echo“预订日期不能为过去!”;
?>
单击以重试!
单击以重试!
在我可以准确放置此代码的地方,我尝试了几次,但它没有显示错误yet@user3573751将其放入第一个if(空($errors)==true)
块中,在底部-选中$\u POST['num\u of_rooms']
后,添加elseif(strotime(…
。有意义吗?如果您需要,我可以给您提供更多详细信息,但您只需将其包含在其余的验证中即可。因此,您可以告诉我在安装代码后代码的外观吗?@user3573751有关实现细节,请参阅我的更新。
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' ;
?>