简体   繁体   English

SQL / PHP如何验证日期是否在日期范围之间?

[英]SQL/PHP How can I validate if a date is between a range of dates?

I currently have a bookings and rooms table: 我目前有一个预订和房间表:

bookings table 预订表 预订表

rooms table 房间表

房间表 2 2

I am trying to make a search room sql query that takes into account the current check in and check out date on a room.I do not know how to implement a WHERE condition to check for rooms both with and without existing bookings record. 我正在尝试制作一个考虑到当前入住和退房日期的搜索室sql查询。我不知道如何实现WHERE条件来检查有无预定记录的房间。 and if a booking record does exist on how can i validate a user input's check in and check out dates so that the query will not output rooms with bookings that clashes with the users input date range 以及是否确实存在预订记录,说明如何验证用户输入的签入日期和退房日期,以便查询不会输出预订与用户输入日期范围冲突的房间

This is my sql so far: 到目前为止,这是我的sql:

$sql = "SELECT rooms.rid, beds, orientation, price FROM rooms JOIN bookings ON rooms.rid = bookings.rid
WHERE (beds = $nOfBeds)
AND ($cInDate < checkin AND $cOutDate <= checkin)
OR ($cInDate > checkout AND $cOutDate >= checkout)";
if ($rOrientation != "") {
$sql .= " AND orientation = '$rOrientation'";
}

right now it is only outputting rooms that has an existing booking and not outputting other rooms that meets the number of beds parameter but without existing bookings. 目前,它仅输出具有现有预订的房间,而不输出满足床位数参数但没有现有预订的其他房间。

BETWEEN Operator with Date Value Example 带日期值示例的运算符之间

http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_between_date&ss=-1 http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_between_date&ss=-1

AND ( checkin BETWEEN $cInDate AND $cOutDate ) AND(在$ cInDate和$ cOutDate之间签入)

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

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