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);
Related
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
I would like to know how can I get the leading zero's here. I already tried %H AND %h but still not working.Any help would be appreciable.Thank you
SELECT * FROM events WHERE eventDate = DATE_FORMAT(CURDATE(), '%d-%m-%Y') AND time BETWEEN DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 2 HOUR),'%H:%i') AND DATE_FORMAT(DATE_ADD(NOW(), INTERVAL 10 MINUTE),'%H:%i')
UPDATE:
It just doesn't work between 00:00 and 02:00.. Do someone has any ideia why it's happening??
As you said your problem only exists when time between 00:00 and 02:00, it caused by this code:
DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 2 HOUR),'%H:%i')
Let's break your code into few parts:
select date_format(curdate(), '%d-%m-%Y') as date_only,
DATE_FORMAT(DATE_SUB(now(), INTERVAL 2 HOUR),'%H:%i') as min2h,
DATE_FORMAT(DATE_ADD(now(), INTERVAL 10 MINUTE),'%H:%i') as add10m
# assume now = 2016/10/28 01:00
# the date_only will return 2016/10/28
# the min2h will return 23:00
# the add10m will return 01:10
back to your query, you will have a query like this:
SELECT * FROM events WHERE eventDate = '20161028' AND time BETWEEN '23:00' AND '01:10'
That's why you can't get the result which the statement is not correct. You should convert your eventDate and time to datetime first, so that you can compare the datetime correctly when minus 2 hours is yesterday or add 10 minutes is tomorrow
select * from events
where str_to_date(concat(`eventDate`, " ", `time`), '%d-%m-%Y %H:%i') # convert o datetime, depends on your format
between date_sub(#report_date, interval 2 hour)
and date_add(#report_date, interval 10 minute)
Fiddle
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
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' ;
I have a booking in my mysql booking table bookings as below:
start_date = 2013-03-04
end_date = 2013-03-08
I want to check if a particular day (in this case 2013-03-04) falls between the start and end dates in the booking(s) - BUT i want to add one day to the start date and subtract 1 day from the end date.
So instead of searching between 2013-03-04 and 2013-03-08
i want to search between 2013-03-05 and 2013-03-07
The following query below does the subtract 1 day from the end date but keeps the start date as 04. The search below should give no results but it is still using 04 as the start date and giving my the result of that booking. It is basically searching between [04] [05] [06] [07] when it should do [05] [06] [07]
$fd_query_params = array(
':day' => '2013-03-04
);
$query = "
SELECT
*
FROM
bookings as bb
WHERE
:day BETWEEN
DATE_ADD(bb.start_date, INTERVAL 1 DAY)
AND
DATE_SUB(bb.end_date, INTERVAL 1 DAY)
";
Not php expert myself but should there be more than I notice there is only a single quote around the :day string. It would be good to see the output to verify what the start_date and end_date is