Laravel Get rows by time for all dates - php

I'm trying to get "Usage Patterns"
So I need to extract from my db all the rows from the last 30 days but only the ones that created at
7:00 - 14:00 for example
the values is from "created_at" so its datetime
I thought just to extract all and check str_contais for 14:00 or something
But I'm sure its not the best idea

You should be able to combine DATE and TIME queries to get what you need.
->whereBetween(DB::raw('DATE(created_at)'), [$from_date, $to_date])
->whereBetween(DB::raw('TIME(created_at)'), [$from_time, $to_time])
Example SQL
SELECT * FROM table WHERE
DATE(created_at) BETWEEN '2019-09-22' AND '2019-10-22' AND
TIME(created_at) BETWEEN '07:00:00' AND '14:00:00'

Related

I want to have a column in MySQL database that automatically calculates difference in DATETIME

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.

how to separate Day and Night Record using Mysql Statement

In my SQL database I store date with time stamp for example like this(2015-09-21 18:02:14) I have requirement to get last time(18:02:14) is Day or Night using SQL statement
If you have another idea please share with me. I would like to use it if it fits my requirements.
In my table if have 20 record same date then get only day record how to create query like that
If the day defines from 6am to 6pm, Then
SELECT column
FROM `tablename`
WHERE HOUR( column )
BETWEEN 6
AND 18;
First you should define what is a day and night. Then you can use DATE_FORMAT function to convert datetime field to HH:MI string.
For example you can select records from 18:00 to 8:00 (Night)
select * from t
WHERE DATE_FORMAT(dt, '%H:%i')>='18:00'
or DATE_FORMAT(dt, '%H:%i')<'09:00'
SQLFiddle demo
You should have another table (or other data source) that supplies the sunset and sunrise times, then compare you own datetime, to that source. You can compare either before adding to your table (and make another column named isDay) or when SELECTing from the table.
Note:
Sunset/Sunrise times depend on your geo-location.
There are API's that can provide that info
There are algorithms that can assist in calculating that info
Examples:
http://sunrise-sunset.org/api
http://api.sunrise-sunset.org/json?lat=36.7201600&lng=-4.4203400&date=2015-12-13
I need a sunrise/sunset webservice api
-- from sql server 2008 until now
SELECT
CASE WHEN DATEPART(HOUR, GETDATE()) < 18 THEN 'DAY'
ELSE 'NIGHT'
END
-- from sql server 2012 until now, if you don't like CASE
SELECT IIF(DATEPART(HOUR, GETDATE()) < 18, 'DAY', 'NIGHT')
18 = 6PM
you can replace GETDATE() with your DATETIME column
SELECT
CASE WHEN DATEPART(HOUR, 'yourDateTimeColumn') < 18 THEN 'DAY'
ELSE 'NIGHT'
END AS PeriodOfDay
FROM 'yourTable'

Mysql query or code for time range search

Is there anybody who write query for time range. I am searching from last 6 days when I get the result it will all about dates. Where are times?
I need a solution for searching e.g. from 12:00:00 PM to 04:00:00 PM in datetime field. So how can I search that from mysql table on whatever dates the have. But the result will how on that time range. Any body have solution for that.
Use the TIME() function, it extract the time portion from a datetime field https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_time
SELECT TIME(datetime_col) FROM table
WHERE TIME(datetime_col) BETWEEN '12:00:00' AND '16:00:00'

Select all records between two dates created between time of day - MySQL

I'm using Laravel 3 - PHP and MySQl.
I need to select all records created between two dates (created_at DATETIME) and between two times (3pm and 6pm).
$query->where_between('created_at', $this->s['start'], $this->s['end']);
How can I add a time range to that for between 3pm and 6pm between those days?
Edit
$this->s['start'] = '2014-2-14';
$this->s['end'] = '2014-2-16';
Timerange: 06:00:00 and 09:00:00 UTC
Dates are stored in Datetime like 2014-2-15 06:43:56
Example data
Records:
Between 2014-2-14 and 2014-2-17 AND between 07:00:00 and 14:00:00.
2014-2-15 06:43:56
2014-2-16 08:43:56*
2014-2-17 15:43:56
2014-2-17 10:43:56*
2014-2-18 12:43:56
2014-2-19 14:43:56
2014-2-20 16:43:56
* selected record
Along the lines of:
$query->raw_where('EXTRACT(HOUR_SECOND from `created_at`) between 070000 and 140000');
Simpler way to do this?
Why are you not doing this using sql? Filtering is part and parcel of SQLs raison d'etre. Indeed, the between clause exists for precisely this reason:
SELECT pkid from tbl where datetime_field between 7:00:00 and 14:00:00
I'd recommend keeping the filtering as close to the database as is reasonably possible, given the constraints of the database itself.
I think that you just have to add an other where_between constraint to your request :
$query->where_between('created_at', $this->s['start'], $this->s['end'])
->where_between('created_at', '6:00:00', '9:00:00');
Never tried but it should do the job !
Thanks to hd1 for SQL tip.

Mysql intersection of datetimes

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

Categories