简体   繁体   English

Laravel 5.1获取start_time和end_time之间的行(两个不同的字段)

[英]Laravel 5.1 Fetch Rows between start_time and end_time (two different fields)

I've been working on reservation project, I collect request (time_start and time_end) and add 1:30 hour to each so i can prevent user to select conflicts. 我一直在做预订项目,我收集请求(time_start和time_end)并且每个都添加1:30小时,这样我就可以阻止用户选择冲突了。

I tried using "Between" already but it doesnt seems to work. 我尝试过使用“之间”但它似乎没有用。

->whereBetween('reservation_time_start', [$time_from, $time_to])
->whereBetween('reservation_time_end', [$time_from, $time_to])

I also tried this, 我也尝试过这个,

->whereRaw('((reservation_time_end <= ? AND reservation_time_end >= ?) OR (reservation_time_start >= ? AND reservation_time_start <= ?))',[$time_end, $time_start,$time_start, $time_end])

It work but in some cases like between some time range, i dont get any results. 它工作,但在某些情况下,如某些时间范围之间,我没有得到任何结果。 Like This. 像这样。

+----+------------------------+----------------------+
| id | reservation_time_start | reservation_time_end |
+----+------------------------+----------------------+
|  1 | 10:00:00               | 13:00:00             | <- i need to get this
|  2 | 12:00:00               | 14:00:00             |
|  3 | 14:00:00               | 15:00:00             |
+----+------------------------+----------------------+

sample user input's: 示例用户输入:

$reservation_time_start = 12:00:00 (-1:30 Hour)
$reservation_time_end = 14:00:00 (+ 1:30 hour)

and when i execute my code, it returns null. 当我执行我的代码时,它返回null。 Please help. 请帮忙。

You can use Carbon for your problem, 你可以使用Carbon来解决你的问题,

you have user input times as: 您的用户输入时间为:

$reservation_time_start = 12:00:00;
$reservation_time_end = 14:00:00;

I suggest you to use date and time, so your user input should be, 我建议你使用日期和时间,所以你的用户输入应该是,

$reservation_time_start = 2017-07-18 12:00:00;
$reservation_time_end = 2017-07-18 14:00:00;

Now, parse these times with Carbon. 现在,用Carbon解析这些时间。 After that add and substract 1:30 hour from end time and start time respectively. 之后,分别从结束时间和开始时间加1和减1分钟。

$start_time = Carbon::parse($reservation_time_start)->subMinutes(90);
$end_time = Carbon::parse($reservation_time_end)->addMinutes(90);

After, this you can use simple query like this, 之后,你可以使用这样的简单查询,

DB::table('reservation')->where(function($query)  
     use($start_time,$end_time){      

         $query->where('reservation_time_start','>',$start_time)
               ->where('reservation_time_start','<',$end_time);
     })
    ->orWhere(function($query) use($start_time,$end_time){   

         $query->where('reservation_time_end','>',$start_time)
               ->where('reservation_time_end','<',$end_time);
    })
    ->get();

From the above query you will get reservations in the given range of time. 从上面的查询中,您将在给定的时间范围内获得预订。

Hope you understand. 希望你能理解。

UPDATE UPDATE

In your case, You have to consider following cases in reservations. 在您的情况下,您必须在预订中考虑以下案例。

  1. Reservation Starting during given range of time. 预约从给定的时间范围开始。
  2. Reservation Ending during given range of time. 预约在给定的时间范围内结束。
  3. Reservation Starting and Ending during given range of time. 在给定的时间范围内预订开始和结束。

暂无
暂无

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

相关问题 如何在yii2中使用start_time和end_time获取MySql中已有的记录? - How to fetch already existing records in MySql with start_time and end_time in yii2? PayPal + PHP - 获取收款(开始时间/结束时间) - PayPal + PHP - Fetch incoming payments (start_time / end_time) 7个SQL行中的第一个开始时间和最后一个结束时间,仅显示1个SQL结果 - First start_time and last end_time from 7 SQL rows to show only 1 SQL result sql使用start_time和end_time进行复杂数据排序 - sql Complex data sorting with start_time and end_time 我想显示当前时间不在(column)start_time和end_time(column)之间的所有人员记录 - I want to display all personnel records that the current time is not between to the (column)start_time and end_time(column) 我想要`start_time` 和`end_time` 计数如何得到这个? - I want to `start_time` and `end_time` count how can get this? 使用 PHP 从具有 start_time 和 end_time 的多维数组中查找总小时数? - Find total hours from multi-dimensional array with start_time and end_time using PHP? 获取每个日期 PHP 的 start_time 和 end_time 的总小时数? - Get total hours from start_time and end_time for each date PHP? 如果在范围内找到,如何找到 start_time 和 end_time 范围,然后检查 start_date 和 end_date - How to find the start_time and end_time range if found in the range then check start_date and end_date Laravel5.1:验证时间字段(开始时间应大于结束时间) - Laravel5.1: validate time field (start time should be greater than end time)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM