2

设想

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)*/
4

3 回答 3

1
if(StartTime - BookingTime < 0 && BookingTime - EndTime < 0)
{
  // Booking time is already taken
}

您可以使用 TIMEDIFF() 在 SQL 中执行此操作。

于 2012-10-08T12:07:21.710 回答
0

我正在研究类似的东西,也许是一种更简单的编码方法,它不是使用时间而是使用时间段?我想这样做的方式是桌子预订(日期,槽ID,房间)桌子槽(可能有槽ID和时间)并且每次预订使用一定数量的槽..然后当你寻找房间什么时候可用它会按日期向您显示哪些插槽是免费的.. 只是一个想法。

于 2012-10-08T12:06:11.110 回答
0

基本上我认为您需要将第一个可用的 room_no 分配给您的 abc-xyz 时间跨度。因此,您应该获取不在已预订集合中的第一个良好价值。示例查询可能是这样的

select room_no
  from
  bookings
  where
  room_no not in (
    select
    room_no
    from bookings
    where start_time >= 'abc' and end_time <='xyz'
  )
  limit 1
于 2012-10-08T12:33:01.897 回答