I have shops that open at a specific time and close at a specific time and I want to filter these stores according to a time period specified by the user so that all the returned shops will be available (opened) at any moment within filtering interval
The shop opens at 10 pm and closes at 2 pm, I mean the shop opens at 10 pm and closes the next day at 2 Pm
I think this is doing the job properly
/* @param filterStart the beginning of the time period selected by the user in minutes from 00:00. @param filterEnd the end of the time period selected by the user in minutes from 00:00. @param start the beginning of the period of availability of the shop in minutes from 00:00. @param end the end of the period of availability of the shop in minutes from 00:00. */ bool ok(int filterStart, int filterEnd , int start, int end) { if (filterStart > filterEnd) filterEnd += 24 * 60; if (start > end) end += 24 * 60; if (start <= filterStart && filterEnd <= end) return true; if ((start <= (filterStart + 24 * 60)) && ((filterEnd + 24 * 60) <= end)) return true; return false; }
I combined the two days on a range (0 to 48 hours) and tested if the shop that open in the first day or open in the second day (the first day + 24 hours)
But I want to use this function to fetch records from database using Laravel.
Can I pass a some function in the WHERE statement in Query Builder to decided it?, Or what is the way to do it?
Advertisement
Answer
Edited my answer after your last comment.
Assuming you can write your C++ function in PHP.
function ok(int $filterStart, int $filterEnd , int $start, int $end): bool { if ($filterStart > $filterEnd) $filterEnd += 24 * 60; if ($start > $end) $end += 24 * 60; if ($start <= $filterStart && $filterEnd <= $end) return true; if (($start <= ($filterStart + 24 * 60)) && (($filterEnd + 24 * 60) <= $end)) return true; return false; }
// User inputs. I wrote 'X' because I don't know enough of your app. $filterStart = X; $filterEnd = X; // Gets all elements of the table 'items' $items->table('items') ->all() // Or you can already filters rows with 'where()' in place of 'all()' if you think it is better: // ->where('start_time', '>=', $filterStart) // ->where('end_time', '<=', $filterEnd) // ->get() // Do for each elements... ->each(function($item, $i) { $isOk = ok($filterStart, $filterEnd, $item->start_time, $item->end_time); if ($isOk) { // Operations to execute if return value is TRUE } else { // Operations to execute if return value is FALSE } });
I hope it will help you a bit at least!