PHP Mysql Get Gap between time range - php

Im kinda new here in stackoverflow. Anyway here is my problem, hope someone could help me on this.
I have a table in mysql see below
Start Time End Time Date
9:00 10:00 2016-02-26
9:00 10:00 2016-02-25
11:00 12:00 2016-02-25
12:00 13:00 2016-02-25
13:00 14:00 2016-02-25
15:00 16:00 2016-02-25
what I want to get is the gap between those time with specific date. See below for my desired output:
Start Time End Time Date
10:00 11:00 2016-02-25
14:00 15:00 2016-02-25
Please if someone know the answer, it would be a big help. Thanks

You can use the following query:
SELECT gap_start, gap_end
FROM (
SELECT (SELECT EndTime
FROM mytable AS t2
WHERE t2.EndTime < t1.StartTime
ORDER BY t2.EndTime DESC LIMIT 1) AS gap_start,
StartTime AS gap_end
FROM mytable AS t1) AS t
WHERE gap_start IS NOT NULL
The correlated subquery used will fetch the StartTime of the records that immediately precedes the current record. Here it is assumed that there is always a gap between consecutive records.
Demo here
Edit:
The query can be modified like as follows in order to handle the addition of a Date field:
SELECT gap_start, gap_end, `Date`
FROM (
SELECT (SELECT EndTime
FROM mytable AS t2
WHERE t2.`Date` <= t1.`Date` AND t2.EndTime <= t1.StartTime
ORDER BY t2.`Date` DESC, t2.EndTime DESC LIMIT 1) AS gap_start,
StartTime AS gap_end,
`Date`
FROM mytable AS t1) AS t
WHERE gap_start IS NOT NULL AND gap_start < gap_end
Demo here

Related

Retrieve records that have expired using the where clause in MySQL and different columns by date and time

Here is my current mysql table
id
Name
expiry_date
expiry_time
1
Sample1
09-01-2023
11:00 AM
2
Sample2
09-01-2023
3:00 PM
3
Sample3
08-01-2023
10:00 PM
Query:
select *
from table
where STR_TO_DATE(table.expiry_date, '%d-%m-%Y') < '2023-01-09'
OR (DATE_FORMAT(concat(table.expiry_date,' ',table.expiry_time),'%d-%m-%Y %g:%i %A') <= NOW()))
I also wish to obtain the records that are out-of-date as of the present time.
How to do that?
You can use STR_TO_DATE to convert your varchar date as follows :
select *
from _table
WHERE STR_TO_DATE(concat(_table.expiry_date,' ',_table.expiry_time), '%d-%m-%Y %h:%i %p') <= now();
you can check it here : https://dbfiddle.uk/emR4cg3s

How to SELECT a date between two dates

How can I select from mysql with php a range between 2 dates?
The working day, start for example, at 7:00 am of 2022-01-01 and ends at 2:00 am of 2022-01-02. I want that all collected datas from 00:00 to 2:00 am of 2022-01-02 are grouped inside previous date.
Now I use this query
SELECT date_format(dateAdded,"%m-%d") as mth,COUNT(productID) as total, productID FROM statisticsNW WHERE userID = "35" AND productID = "1193'" AND YEAR(dateAdded) = "2022" AND month(dateAdded) = "01" GROUP by year(dateAdded),month(dateAdded), day(dateAdded) ORDER by year(dateAdded),month(dateAdded)
and the result is this but the "total" is wrong because is from 00:00 to 23:59 of the same day. The correct query the I want is from 7:00 to 6:59 of the next day. But from 00:00 to 6:59 datas must be counted in the previous day.
you can add 5 hours to the start_time and end_time, so the it appears the shift starts at midnight
select DATEDIFF(HH,cast('2022-01-24 19:00:00' as datetime),cast('2022-01-25 00:00:00' as datetime)) as time_5_hrs
select dateadd(HOUR,5,cast('2022-01-02 19:00:00' as datetime)) as starttime, dateadd(HOUR,5,cast('2022-01-03 02:00:00' as datetime)) as endtime,datediff(HH,dateadd(HOUR,5,cast('2022-01-02 19:00:00' as datetime)),dateadd(HOUR,5,cast('2022-01-03 02:00:00' as datetime))) diff
By doing this you can get both date are the same and you will be able to group it as single day.
I have solved
SELECT DATE_SUB(DATE_ADD("2017-06-15 5:30:00", INTERVAL 24 HOUR), INTERVAL 1 MINUTE);

Search record between 3 date ranges

I need to output data from a DB table that selects records between 3 (not 2) date/time ranges
E.g. start time : 2019-09-07 18.00
end time : 2019-09-07 20.00
so the user should be able to see the record 25 minutes before the start date-time (6.p.m - 18.00), during the event but not after the end date-time (8.p.m -20.00).
I've tried
db->query = "SELECT o_id, schedule, date, start_time, end_time FROM working_schedule WHERE o_id = '".$user_id."'
AND (start_time <= '".date('Y-m-d\TH:i:s', strtotime("-25 minutes"))."' AND start_time >= '".date('Y-m-d\TH:i:s')."')
AND end_time >= '".date('Y-m-d\TH:i:s')."'";
but the result is NULL.
For reference HERE'S a sql fiddle.
Thanks in advance for pointing me in the right direction.
Do you need this ?
select * from working_schedule
where
NOW() BETWEEN DATE_SUB(start_time,INTERVAL 25 MINUTE) AND end_time

Check time between two columns in MySQL

I have a MySQL table, booking:
id shop_id date start_time end_time
1 21 2019-1-13 09:00 10:00
2 21 2019-1-13 11:00 11:30
Now before I a insert record into booking table I want to check whether date and time is already booked or not.
How can I do this?
I tried with the following code:
SELECT * FROM booking
WHERE CAST(start_time as date) BETWEEN
'2012-08-30' AND '2012-08-31';
If I understand correctly, you'll have separate values for #date, #start_time, and #end_time that you want to check in the table.
To return all overlaps:
SELECT b.*
FROM booking b
WHERE #date = b.date AND
#end_time >= b.start_time AND
#start_time < b.end_time;
As per your table structure it is better to use
SELECT * FROM booking
WHERE CAST(CONCAT(date,' ',start_time)AS timestamp)
BETWEEN '2012-08-30' AND '2012-08-31';
Otherwise it is better to use full timestamp for both start_time and end_time.

Get count of several times slots on between given two mysql time slots within datetime field

I have several in data and out date mysql datetime values. I need to return total count from given dateperiod and given time slot
Ex.
time
2012-02-01 10:00
2012-02-01 12:00
2012-02-01 14:00
2012-02-02 09:00
2012-02-02 10:00
2012-02-03 11:00
how to get data basis on time and date slot form datetime field as in this question i only want to get data from date 2012-02-01 to 2012-02-02 and time from 09:00 to 12:00 hence this should not provide the data of 2012-02-01 14:00 hope you got this now
you need only to add count(*) in your statement.
SELECT count(*) FROM users WHERE created_at BETWEEN '2012-02-01 09:00' AND '2012-02-02 12:00'
Try this -
SELECT count(*) as c FROM users WHERE created_at
BETWEEN '2012-02-01 09:00' AND '2012-02-02 02:00' AND
Time(c) BETWEEN '09:00' AND '02:00'
2012-02-01 14:00 is rightfully included in the result of your query, what you want to achieve can be done this way
SELECT * FROM users WHERE
(DATE(created_at) BETWEEN '2012-02-01' AND '2012-02-02')
AND
(TIME(created_at) BETWEEN '09:00' AND '12:00')
You have a typo in your query. The closing time is 14:00, not 12:00:
SELECT COUNT(*)
FROM users
WHERE created_at BETWEEN '2012-02-01 09:00' AND '2012-02-02 14:00' ;
EDIT:
If you are trying to limit the times and dates separately, then use two conditions:
SELECT COUNT(*)
FROM users
WHERE created_at >= '2012-02-01' AND created_at < '2012-02-03' AND
time(created_at) BETWEEN '09:00:00' AND '14:00:00' ;

Categories