[SOLVED] Check Time Period from 10PM to 6AM


This Content is from Stack Overflow. Question asked by ruelluna

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?


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'))

This Question was asked in StackOverflow by ruelluna and Answered by Engin It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?