Home > Mobile >  Filter available time slot for today in laravel using carbon
Filter available time slot for today in laravel using carbon

Time:08-03

I want to filter available time for today for a user. He can only make an appointment for a time slot that is bigger than time right now 6 hours. But the problem is when 6 hours transfers to the next day so it could be like 00:44 next day and in that case when it compares to the time slots it shows that time slot at 8:00 for example is bigger than 00:44 so it shows that time slot for today although it shouldn't be possible to make an appointment for that time slot anymore. Is there a way to filter it in the eloquent? I basically want addHours to stop if it reaches 23:59. My Eloquent function:

        $times_today = TimeSlot::where('status', 1)
        ->whereTime('time', '>', Carbon::now()->addHours(6))
        ->get();

CodePudding user response:

Carbon provides a tomorrow() function that you can use in the same way you've made use of now().

I assume you don't want people booking appointments up until midnight (although perhaps you do?) so I would also use the subHours() function to limit timeslots to before a certain time.

$times_today = TimeSlot::where('status', 1)
        ->whereTime('time', '>', Carbon::now()->addHours(6))
        ->whereTime('time', '<', Carbon::tomorrow()->subHours(2))
        ->get();

CodePudding user response:

$dtToronto = Carbon::create('2022','8','2', 23,59,59, 'America/Toronto'); 
$times_today = TimeSlot::where('status', 1)
->whereTime('time', '>', Carbon::now()->addHours(6))
->whereTime('time', '<=', $dtToronto)->get();
  • Related