Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 检查提供的datetime值是否位于特定时间间隔之间_Php_Mysql - Fatal编程技术网

Php 检查提供的datetime值是否位于特定时间间隔之间

Php 检查提供的datetime值是否位于特定时间间隔之间,php,mysql,Php,Mysql,情景 乌德帕特 请忽略评论部分。在考虑了另一种选择后,我想到了这个: 假设我有 $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

情景

乌德帕特

请忽略评论部分。在考虑了另一种选择后,我想到了这个:

假设我有

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

我正在研究类似的东西,也许更简单的编码方法不是使用时间而是使用时间段?我想到的方法是预订一张桌子的日期、插槽ID、房间桌子插槽,可能有插槽ID和时间,每次预订使用一定数量的插槽。。然后,当您查找房间是否有空位时,它会显示每个日期的空位。。只是个主意

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

您可以在SQL和TIMEDIFF中执行此操作。

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

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

所以在我的例子中,bookingtime将是abc引用的时间?我的示例是显示是否使用了指定的时间。所以,如果在下午3点到5点之间有预订,并且预订时间是下午4点,那么它将输入if语句。如果您正在验证一个新的请求,您可能希望比较新的开始预订时间和结束预订时间,以确保它们不会重叠。但这是否意味着要遍历表中的每条记录以获取每行的开始时间和结束时间?如果有300多行,这不是很昂贵吗?如果您正确构建了查询,就不会了。例如,要选择请求的预订时间不可用的所有房间号:从表中选择房间号,其中时间到扇区区分开始时间、$InputStartTime>0和时间到扇区区分时间$InputEndTime>0;我犯了一个错误,现在进行编辑已经太晚了:从表中选择房间号,其中时间到扇区diffstart\u TIME、$InputStartTime<0和时间到扇区diff$InputEndTime、end\u TIME<0;这只检查开始时间和开始时间,结束时间和结束时间。。。你真的想把这两个都检查一下。谢谢你的主意,但我希望避免改变表中存储时间的格式。如果没有别的办法,我就试试你的方法。由准备时间表的用户分配房间。所以这不是第一个可用的房间。他可以选择他想要的任何房间。因此,我试图将该房间从下拉列表中排除,这样他就无法选择它。好吧,在这种情况下,答案应该包含在其中:删除限制子句,您可以为传入的abc、xyz参数获得所有可用房间,然后您可以随心所欲地使用它们