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);
Related
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();
I currently have my database updating the timestamp when new records are inserted. It formats like this:
2017-08-24 15:48:30.189182
My question is: how do I delete records that are over, suppose, 3 days old?
I'm assuming I can run a cron job nightly to remove the records older than 3 days. Can someone give me the format for it? I'm not sure how to work with the timestamp data.
Thank you for your help!
different database has different way to compute time difference between two days...
MySQL: DATEDIFF(date1,date2);
Oracle: select date1 - date2 from dual;
and so on...
Assuming the column is of type DATE, you can run a query that deletes all columns where the difference between today and your column is greater than 3.
Replace table with your table, and date_column with your column-name. Again, this assumes that you've got columns that is of the DATE-type, and not a VARCHAR or something.
DELETE FROM table WHERE DATEDIFF(CURDATE(), date_column) > 3
SQLFiddle
MySQL Date functions
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 want to create a query for fetch all the data from database. But condition is get from before current month and data will convert into 3 days slot wise with count.Like if i have 12 month data and in July 1 to 3 date data will insert into 50 rows and from 3 to 6 date data will insert into 5 rows.How i fetch this things.That is the mail problem. I have wondered but nothing found related my problem. Is this possible.
I assume you have a DATE or a DATETIME column, that marks the day of the insert, say the column created_at. Then you could get the desired result.
SELECT
COUNT(*)
FROM
your_table
WHERE
created_at BETWEEN '2014-07-01' AND '2014-07-31 23:59:59'
GROUP BY
(DAYOFMONTH(created_at) - 1) DIV 3
Explanation:
With the help of the DIV operator you get your 3-day-slots.
Note
If there's a slot without rows, it won't be in the result. To get these too, you could use a LEFT JOIN with the maximum 11 slots.
Demo
If you having timestamps as attribute type then might be this help you
SELECT count(*) as count
FROM table
AND
TIMESTAMPDIFF(DAY,date,2014-08-01)<=3";
The date is the attribute of your column, TIMESTAMPDIFF will find difference between given date if it's under 3 days from 2014-08-01 records will show up. This way by just sending one date into this position you can find 3 slots of date from your table.
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).