简体   繁体   English

检查mysql日期是否在日期之间

[英]Check mysql if date is between dates

i'm learning php at the moment and i wanted to make a car hire system to challenge myself a bit. 我目前正在学习PHP,我想做一个租车系统来挑战自己。

Now i am stuck with a validation question, i got 5 diffrent cars and want to check if the car is available at the dates the user submitted. 现在我遇到了一个验证问题,我有5辆不同的汽车,并想在用户提交日期检查汽车是否可用。

my database table got a startdate, enddate, car and a id 我的数据库表中有一个开始日期,结束日期,汽车和ID

This is what i'm trying to get working: 这就是我正在尝试的工作:

if($_POST){


$date1 = $_POST["startdate"];
$date2 = $_POST["enddate"];
$car= $_POST["car"];

$qry1 = "SELECT * FROM hire WHERE startdate between '{$date1}' and '{$date2}'
UNION
SELECT * FROM hire WHERE enddate between '{$date1}' and '{$date2}'";

$qry2 = "SELECT * from hire WHERE car = '{$car}'";

$results = mysql_query($qry1, $bd);
$results2 = mysql_query($qry2, $bd);
$row  = mysql_num_rows($results);
$row2  = mysql_num_rows($results2);

if ($row && $row2 > 1 ){
   die("it twerks");
}

else {
  //do stuff\
}
}

now it says its always available. 现在它说它总是可用的。

EDIT 编辑

start and enddate are DATE fields format is Ymd. 开始和结束日期均为DATE字段格式为Ymd。

First you want to consolidate all of your query logic to find overlapping reservations into one query without unions. 首先,您想整合所有查询逻辑,以将重叠的保留查找到一个没有联合的查询中。 The below should return available cars for the specified date range. 以下应返回指定日期范围内的可用汽车。

SELECT *
FROM hire
WHERE
  startdate  NOT BETWEEN '{$date1}' AND '{$date2}' OR
  enddate    NOT BETWEEN '{$date1}' AND '{$date2}' OR
  '{$date1}' NOT BETWEEN startdate  AND enddate    OR
  '{$date2}' NOT BETWEEN startdate  AND enddate

Second, you cannot take shortcuts with logic statements like this: 其次,您不能使用类似于以下逻辑语句的快捷方式:

if ( $row && $row2 > 1 )

It must be written like: 它必须这样写:

if ( $row > 1 && $row2 > 1 )

Third, yadda yadda don't use mysql_*() functions. 第三, yadda yadda不使用mysql_*()函数。 [See the pedantry in the comments on your question.] [请参阅您对问题的评论中的脚手架。]

I recommend PDO. 我推荐PDO。 It supports more database systems than just mySQL, and MySQLi has some really strange syntax. 它不仅支持mySQL,还支持更多的数据库系统,而MySQLi具有一些非常奇怪的语法。

Hey Sorry for the late response. 抱歉,回复晚了。 I have done something similar but for a bed and breakfast, Room reservation system. 我做了类似的事情,但对于床和早餐,房间预订系统。

Please kindly start by normalizing your database properly. 请从正确规范化数据库开始。

you can have three tables such as Car, Hire, or Person. 您可以拥有三个表格,例如Car,Hire或Person。 your car table, can have all the description of your vehicle. 您的汽车表,可以包含您所有的车辆描述。 Person table can have all the description of the person hiring the car. 人员表可以包含所有租车人的描述。 your hire table will reference the car_id and person_id. 您的租金表将引用car_id和person_id。 It will also have you start_date and end_date. 它还将具有start_date和end_date。 So like this. 像这样

  • car table. 车表。

car_id (primary key), car_color (text), Car_make(text) car_id(主键),car_color(文本),Car_make(文本)

** **

  • Person table 人表

** person_id (primary key) person_name (text) person_telephone (text) ** person_id(主键)person_name(文本)person_telephone(文本)

Hire table Hire_Id(auto_increment) person_id (foreign key) car_id (foreign key) start_date (date) end_date (date) 雇用表 Hire_Id(auto_increment)person_id(外键)car_id(外键)start_date(日期)end_date(日期)

$Query="SELECT r.* FROM car r WHERE r.car_id NOT IN ( select b.car_id from hire b where NOT (b.end_date< '2014-08-15' or b.start_date>'2014-08-12')) order by r.car_id;" $ Query =“ SELECT r。* FROM car r WHERE r.car_id NOT IN((从租车b选择b.car_id,其中NOT(b.end_date <'2014-08-15'或b.start_date>'2014-08-12 '))通过r.car_id进行订购;”

This return all available cars 这将返回所有可用的汽车

您还可以在DATETIME列上直接使用MySQL比较功能: http : //dev.mysql.com/doc/refman/5.1/en/comparison-operators.html#operator_between

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM