Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

uniqueginun's avatar

check time availability for booking

Hello.

if I have a client model with day, from, to fields that represent their workdays of the week. how can I check if he is available on a certain day/time?

let's say a client name Jon has these records in the database.

mon === 09:40 === 13:40
mon === 17:40 === 22:40
fri === 11:40 === 18:40
wed === 08:40 === 14:40

how to check if Jon is available on mon from 13:30 to 15:30 which must return false.

note that from/to fields are time fields in the database.

0 likes
3 replies
MohamedTammam's avatar

First you need to get the day and the time according to your database design.

$date = new \Carbon\Carbon($check_date);
$day = $date->format('D'); // Day name
$time = $date->format('h:i'); // Time

$isAvailable = Client::where('day', $day)->whereTime('from', '<=', $time)->whereTime('to', '>=', $time)->count() > 0;
uniqueginun's avatar

@MohamedTammam Actually I used whereDate at first and it wasn't working. now when I replaced it with where it's actually working. here's my code

public function scopeOnDay(Builder $builder, string $dayName)
    {
        $builder->whereDay($dayName);
    }

    public function scopeWithin(Builder $builder, string $fromTime, string $toTime)
    {
        $builder->where(function ($query) use ($fromTime, $toTime) {
            $query->where('from', '<=', $fromTime)->where('to', '>=', $toTime);
        });
    }

public function availableAt(Carbon $from, Carbon $to): bool
    {
        return Client::query()->onDay('mon')
        ->within($from->format('H:i'), $to->format('H:i'))
        ->exists();
    }

MohamedTammam's avatar
Level 51

@uniqueginun I'm sorry, it should be whereTime, I updated my answer.

PS: I suppose the column type is time.

If you question got answered, please close the discussion.

Please or to participate in this conversation.