How to query timeslot between 2 times in PHP / laravel

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to query timeslot between 2 times in PHP / laravel



I need help in this logic, I want the user to create an appointment when he pick on a date and apply on a form but if the user takes a lot of time on the page, and someone else book before him for the giving date, he need to see error and go back to pick a date.



So here is my query


$result = Appointment::where('user_id', $post['user_id'])
->where('appointment_datetime', $post['bookingdate'] )
->whereBetween('appointment_time_start', [$begintime, $endtime])
->WhereBetween('appointment_time_end', [$begintime, $endtime])->get();


if ($result->isEmpty()) /// Insert to database ///

else /// return back()->with('msg', 1); ///



It seems that if I want to make appointment from 12:00 to 13:00

and I have an appointment in database from 11:00 to 13:00 , the query is not detecting it.



Also if I have 12:00 to 14:00 the query is not detecting it.



Can someone tell me what is wrong with my query, thank you a lot !





Does have any limitation at the appointment time? Or can be?
– Mesuti
21 mins ago




1 Answer
1



My thoughts are you can iterate at the application the time of appointment on the relevant date.



Firstly I prefer to use Carbon package. You can be install If you don't have.


$appointments = Appointment::where('user_id', $post['user_id'])
->where('appointment_datetime', $post['bookingdate'])
->get();

if ($appointments->count() > 0)
$foundFlag = false;
$beginDate = CarbonCarbon::create(date('Y-m-d') . ' ' . $begintime); // You can be add ":00" if not exists
$endDate = CarbonCarbon::create(date('Y-m-d') . ' ' . $endtime); // You can be add ":00" if not exists
foreach($appointments as $appointment)


if (! $foundFlag)
// Insert Database
else
/// return back()->with('msg', 1); ///






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS