UDPATE
請忽略的註釋部分。思考替代後,我想出了這個:
比方說,我有
$date = '2012-10-03 13:00:00'
的時間間隔範圍是
2012-10-03 12:00:00 to 2012-10-03 14:00:00
現在$date
爲在上述時間範圍之間。有關如何比較日期時間和日期時間範圍的任何想法?我遇到過比較正式日期或正式時間但不是同時比較的函數。任何幫助非常感謝。
/*I'm building a school timetable and want to make sure that a room cannot be assigned to two different periods if it is already occupied. I have datetime values of **`2012-10-03 13:00:00`** (the start time of a period. Let's call it **abc** for reference) and **`2012-10-03 13:30:00`** (the end time of a period. Let's call it **xyz** for reference).
My database table contains columns for room number assigned for a period and the start and end time of that period. Something like this:
room_no | start_time | end_time
5 2012-10-03 13:00:00 2012-10-03 14:30:00
This means for October 3, 2012 room 5 is occupied between 1pm and 2:30pm. So the datetime values that I have (abc & xyz) will have to be assigned to a room other than 5.
I'm at a loss of ideas on how to go about validating this scenario, i.e. make sure that the period with time interval between abc & xyz cannot be assigned room number 5.
Any help would be appreciated. Thanks in advance
PS : I'm not asking for code. I'm looking for ideas on how to proceed with the issue at hand. Also, is there a way a query can be build to return a row if `abc` or `xyz` lie between `start_time` and `end_time` as that would be great and reduce a lot of workload. I could simply use the number of rows returned to validate (if greater than 0, then get the room number and exclude it from the result)*/
所以在我的情況下,預約時間將由'abc'引用的時間? – asprin
我的例子是顯示是否已經過指定的時間。所以如果在下午3點到5點之間預訂,預訂時間是下午4點,它將進入該語句。如果您正在驗證新請求,則您需要比較新的預訂時間和結束預訂時間,以確保它不會重疊。 – MatthewMcGovern
但是,這是否意味着循環遍歷表中的每條記錄以獲取每行的開始時間和結束時間?如果說有300多行,這不會很昂貴嗎? – asprin