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'))
->get();
Related
I want to calculate particular user's total online time for selected date. Below is MySQL table -
User Active Status Table
In status column I store values as 0 (Offline) and 1 (Online).
As you see user_id 33 is online at -
10:05:25
14:00:00
and offline at -
11:45:58
19:00:04
So, How to calculate total online time using Laravel?
Thanks in advance!
You can try to refactor a bit your database..
Add 3 columns date, logged_in_time, logged_out_time and you can make an event listener each time user logs in our out. Any time they will log in our out it will be stored in your table and you can filter easily by dates in your query.
If you want to calculate the amount of time they were logged in for a specific date you will just do 'logged_out_time - logged_in_time'
In MySQL database, I have the following table that stores work clock in and clock out times. I would like the hours column to auto calculate the DATETIME difference in the database.
ID Clock In Clock Out Hours
1 10:00 17:00 7
2 09:00 16:00 7
3 09:00 15:30 6.5
The SQL statement im using to preview the results is:
SELECT TIMESTAMPDIFF(hour, `clock_in`, `clock_out`) as `difference` FROM records
I just want to know how i can apply this to the hours column in the db to auto populate when records are created.
The simplest way to do what you want is to use a view:
CREATE VIEW v_records AS
SELECT v.*, TIMESTAMPDIFF(hour, `clock_in`, `clock_out`) as `difference`
FROM records;
This conveniently calculates the value when it is used. That ensures that the most recent data is used for the calculation.
If you want to get fancy, you can write a trigger. However, you need to deal with both updates and inserts.
I have a backend in Laravel 5.1 where I have 2 columns in my table: seconds and date.
I have to implement a function that select records where date is between Carbon::now() and Carbon::now()->addMinute(60)-seconds where seconds is a column in my database.
So select records if now is 07/11/2016 11:00 and in my table I have date 07/11/2016 10:50 and in my record second have 600.
So far I have tried this query:
DB::table('table')->whereBetween('table.date', array(Carbon::now() , Carbon::now()->addMinute(60)))
But how can I subtract second from a field table?
You can try like this :-
For you reference link
echo $dt->subSeconds(61);
I have the table Vacation in mysql DB. The table has datetime_from and datetime_to. To have a vacation from Monday to Friday means there is only one record in the table with two timestamps
date_from = 'MONDAY_DATE 00:00:00'
date_to = 'FRIDAY_DATE 23:59:59'
The working time is from 8:00 to 16:00 every day. I would like to get all the working time that employee missed during his vacation (in hours for example). It's 8hrs a day x 5.
Im able to do that in 5 queries (one for every day and then sum up with PHP) as an intersection of date intervals BUT is it possible to perform it in only one mysql query?
SELECT SUM(date_to - date_from) as seconds_missed FROM Vacation
WHERE ...
That would give you the total number of seconds missed for an employee assuming the where clause matches it against a given user. I guess you could also add conditions in your where clause to only grab dates in a certain range (i.e. worked missed that week).
Current MySQL table schema: "Date" column with yyyy-mm-dd values
Assuming you are building a web app to rent out XBox's. Would you:
Create a a few checkboxes with "Every Monday," "Every Tuesday," "etc..." (Implication: If it's every Monday, how would you insert dates of only Monday's into the DB? Perhaps only insert the Monday's for the next three months initially and auto-increment and keep the tables light?"
Use a multi-datepicker for users to select multiple dates (Implication: User experience drops since the user will need to select more dates as time progresses?)
Other options?
How would add "hours" in addition to dates?
Store the data separately:
Start date - the start of the range
End date - the end of the range
Days of the week - the days of the week during the range (store this as a JSON array)
Hours per day - the hours per day (store this as a JSON object, with either start/end time per day of the week)
Try taking a look at Google Calendar add event page. They organize the times very smartly. It should give you some ideas for it.