Z
Z
Zubastik_12018-03-18 13:20:35
PHP
Zubastik_1, 2018-03-18 13:20:35

How to make a selection in a loop?

I made a selection of data on time intervals, for example today0_3 - today from zero hours to 3 hours, etc. Is it possible to somehow optimize this code, for example, make it in a loop? I can't figure out how.

$today0_3 = \App\Models\Timer::where('time', '>=', date('Y-m-d').' 00:00:00')->where('time', '<=', date('Y-m-d').' 03:00:00')->get();
$today3_6 = \App\Models\Timer::where('time', '>=', date('Y-m-d').' 03:00:00')->where('time', '<=', date('Y-m-d').' 06:00:00')->get();
$today6_9 = \App\Models\Timer::where('time', '>=', date('Y-m-d').' 06:00:00')->where('time', '<=', date('Y-m-d').' 09:00:00')->get();
$today12_15 = \App\Models\Timer::where('time', '>=', date('Y-m-d').' 12:00:00')->where('time', '<=', date('Y-m-d').' 15:00:00')->get();
$today15_18 = \App\Models\Timer::where('time', '>=', date('Y-m-d').' 15:00:00')->where('time', '<=', date('Y-m-d').' 18:00:00')->get();
$today18_21 = \App\Models\Timer::where('time', '>=', date('Y-m-d').' 18:00:00')->where('time', '<=', date('Y-m-d').' 21:00:00')->get();
$today21_24 = \App\Models\Timer::where('time', '>=', date('Y-m-d').' 21:00:00')->where('time', '<=', date('Y-m-d').' 24:00:00')->get();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lazy @BojackHorseman PHP, 2018-03-18
Tag

it will be correct to make one request with the appropriate grouping

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question