Check Time Period from 10PM to 6AM

Viewed 49

So I have this time-log entry on my mysql database.

TABLE: timelogs
- id
- employee_id
- in (datetime)
- out(datetime)

I wanted to check the entries if there times in between 10PM to 6AM.

This is to calculate the night premium for each employee.

Say a sample entry,

 - in: 2022-09-09 19:34:00
 - out: 2022-09-10 01:29:00

This should result that this time-log has the range to calculate the night premium.

How do I check it using Eloquent?

1 Answers

You can simply use whereBetween clause and convert the columns with date times to time format using TIME function.

Here's an example assuming your model is called Timelogs.

Timelogs::whereBetween(DB::raw('TIME(in)'), array('22:00:00', '06:00:00'))
->orWhereBetween(DB::raw('TIME(out)'), array('22:00:00', '06:00:00'))
->get();
Related