我是用时间戳做的.我知道MySQL内置了DATETIME字段类型,但我通常更喜欢使用时间戳.无论如何,这是我将如何去做
vehicles 表:
id | name
---------
1 | Ford Mustang
Run Code Online (Sandbox Code Playgroud)
reservations 表:
id | vehicle_id | start_time | end_time
----------------------------------------
1 | 1 | 130500040 | 130599304
Run Code Online (Sandbox Code Playgroud)
其中start_time和end_time是UNIX时间戳.
然后,当您想要查看某个日期是否有汽车(在查询中根据哪辆汽车更改vehicle_id)时:
$start_date = strtotime($start_date);
$end_date = strtotime($end_date);
$query = mysql_query("SELECT * FROM `reservations` WHERE `vehicle_id`=1 AND (`start_date`>".$start_date." AND `end_date`<".$end_date.") OR (`start_date`<".$start_date." AND `end_date`>".$end_date.") OR (`start_date<".$end_date." AND `end_date`>".$end_date.") OR (`start_date`<".$start_date." AND `end_date`>".$start_date.")");
if (mysql_num_rows()<1)
{
echo "The vehicle is available";
}
Run Code Online (Sandbox Code Playgroud)