【问题标题】:I want to fetch the data between start_time and end_time using Laravel [duplicate]我想使用 Laravel 获取 start_time 和 end_time 之间的数据 [重复]
【发布时间】:2021-08-14 11:25:08
【问题描述】:

我想从表中获取当前日期以及 start_time 和 end_time 之间的数据

我的表结构是这样的

id    questions       answers    start_date      start_time     end_date      end_time

01     Testing?       Testing    2021-08-14      15:30:00       2021-08-14    17:00:00

我的查询是

$arr["questions"] = questions::whereDate('start_date','=', date('Y-m-d'))
                            ->whereDate('start_time','>=', now()->format('H:i:s'))
                            ->whereDate('end_time','<=', now()->format('H:i:s'))->first();


select * from `questions` where date(`start_date`) = '2021-08-14' and date(`start_time`) >= '16:50:45' and date(`end_time`) <= '16:50:45' limit 1

这个查询没有给出任何东西。我不知道是什么问题。请帮我解决这个问题。

【问题讨论】:

  • 你有 >= 开始和结束时间。

标签: php mysql laravel eloquent


【解决方案1】:

在您的查询中,start_time 大于当前时间,end_time 也大于当前时间。您需要更改 end_time 查询中的运算符。 end_time 应小于或等于当前时间。您可以在查询中使用它。

whereDate('end_time','&lt;=', now()-&gt;format('H:i:s'))

更新的答案(使用数据库查询生成器): 不幸的是,我们在检查时间时出错了。 current_time 应大于或等于 start_time,current_time 应小于或等于 end_time。这是更新后的查询

$current_time = now()->format('H:i:s');
$questions = DB::table('questions')
    ->whereDate('start_date', '=', now()->format('Y-m-d'))
    ->whereTime('start_time', '<=', $current_time)
    ->whereTime('end_time', '>=', $current_time) ->first();
dd($questions);

【讨论】:

  • $arr["questions"] = questions::whereDate('start_date','=', date('Y-m-d')) ->whereDate('start_time','>=' , now()->format('H:i:s')) ->whereDate('end_time','format('H:i:s'))->first( );
  • 我试试这个也没有给出任何结果
  • 我认为,您当前的时间不在 start_time 和 end_time 之间,这就是您没有得到任何结果的原因。
  • select * from questions where date(2021-08-14) = '2021-08-14' and date(15:30:00) >= '17:03:45' and date(@987654326 @)
  • 像这样在我的表中,我当前的时间过去了 17:05:00 结果应该是空的
猜你喜欢
  • 1970-01-01
  • 2021-06-07
  • 1970-01-01
  • 2020-11-17
  • 1970-01-01
  • 1970-01-01
  • 2020-05-09
  • 1970-01-01
  • 2021-06-18
相关资源
最近更新 更多