mysql test if time is within a time span over midnight - mysql

I am working on a mysql query that will search a database of entries that each have a time span to see if a user input time or time span falls within the entries spans.
Ex user can input "13:00" or "13:00-16:00"
the database has entries like so:
id startTime endTime
1 08:00 12:00
2 20:00 03:00
3 14:00 01:00
4 16:00 21:00
Searching is easy enough against a time span that is during a single day (startTime < endTime). The issue is testing when a span goes across midnight (endTime < startTime). For this application these values can not have a date attachment (they are stored as time only), so timediff etc will not work.
UPDATE:
The time spans will never be greater than 24 hrs so startTime of 1 and endTime of 3 will always be a 2 hr item, not a 26 hr item.
The reason I am not using dates is this relates to something more or less like business hours (but not exactly that use case). The items the spans represent have daily hours, so it is not practical to add datetime stamps for every day.

I would check if endTime
totaltime = startTime + (24 hours - endTime)
The problem I see with this is if the endTime is the next day and endTime>startTime. For example you start at noon and end at 1pm the next day. There is also a problem if the time spans more than 2 days. For example start on day 1 and end day 3 or longer. I would recommend using dates.

Related

How to get minutes difference between two TIME type attributes in mysql [duplicate]

I need to calculate the difference between two columns which are of type time. The fields are named start_time and end_time. I use the function timediff(s,e) and it works well but some times it doesn't. For example when end_time is 00:00 which is considered as 12 AM and start_time is 19:00 which is 7 PM the difference function shows 19 hours (actually I also select hour(timediff(s,e))) while I expect it to be 5 hours.
How can I fix this?
I tried this
case
when finish_time < start_time then TIMEDIFF(timestamp('2021:01:02',finish_time),timestamp('2021:01:01',start_time))
else TIMEDIFF(timestamp('2021:01:01',finish_time),timestamp('2021:01:01',start_time))
end as diff,
and it works
As 00:00:00 is considered the start of a new day or even if you have an end time that is the day after the start time you will have to make your start and end field into DATETIME types. Also if you want the difference between the 2 times you should be using timediff(end,start)

Mysql: calculate length time between two times in AM and PM

I need to calculate the difference between two columns which are of type time. The fields are named start_time and end_time. I use the function timediff(s,e) and it works well but some times it doesn't. For example when end_time is 00:00 which is considered as 12 AM and start_time is 19:00 which is 7 PM the difference function shows 19 hours (actually I also select hour(timediff(s,e))) while I expect it to be 5 hours.
How can I fix this?
I tried this
case
when finish_time < start_time then TIMEDIFF(timestamp('2021:01:02',finish_time),timestamp('2021:01:01',start_time))
else TIMEDIFF(timestamp('2021:01:01',finish_time),timestamp('2021:01:01',start_time))
end as diff,
and it works
As 00:00:00 is considered the start of a new day or even if you have an end time that is the day after the start time you will have to make your start and end field into DATETIME types. Also if you want the difference between the 2 times you should be using timediff(end,start)

group by by considering 4am this day to 4am next day as this day (my SQL)

The data base I am querying has 90 days of data .
I want to get a result where
The day starts from 6/22/2021
eg 4 am (7/1/2021) and ends in 4am (7/2/2021)-this should be considered as 7/1/2021
4am (7/2/2021) to (4am 7/3/2021) as 7/2/2021
Baisically I want to group 4am this day to the next day 4am so that I can get 90 days of data in 1 go.
I used cast(dateadd(hour,-4).... but it didnt work as there is an overlap from 12 am to 4 am
Subtract 4 hours from the time and then group by date.
SELECT DATE(DATE_SUB(timestamp, INTERVAL 4 HOURS)) AS date, ...
...
GROUP BY date

Grouping by Month i.e. 30 days from the starting timestamp rather than the actual month

I have a monthly offering that gives customers 30 minutes of use every month and I need to tally up their usage and group it by month, but not sure the best way to handle that since the start date could be any day of the month.
Should I prorate? Convert into full months or count the days?
Is there an ideal way to tally this? Let's say each month they get 30 minutes of use time, so I want to tally how many minutes were used that "month."
Say there is a minutes used table and the start date for the subscription is 2019-06-13 00:00:00
minutes_used_table:
Userid MinutesUsed Date
1 5 2019-06-19
1 6 2019-06-23
1 8 2019-06-28
1 15 2019-07-05
1 3 2019-07-12
1 8 2019-07-19
1 5 2019-08-14
1 3 2019-08-22
1 1 2019-08-26
1 2 2019-09-13
Or, should I prorate it and instead of tracking in 30 day increments, if they start on the 13th of June, should I just count the days from the start date to the end of the month, whatever day that is and then the days from the first of the month until the start date?
Wondering what makes the most sense and how to carry it out?
Use
GROUP BY DATEDIFF(`Date`, #starting_date) DIV #period_length
In your particular case it will be
GROUP BY DATEDIFF(`Date`, '2019-06-13 00:00:00') DIV 30
From coding point of view it's not a big deal to find first date for user, subtract it from dates and count number of 30-days periods.
But this will be very hard to support in future and this solution will have a number of corner cases: for example someone first start to use product at 20 January, after month of usage have been idle for a while and came back in April, 5. What will be right start date for that case?
So I suggest to use calendar months. And may be reduce limit on first month accordingly to the number of days left or give full trial period even on first month as user has ability to spend 30 minutes.

Checking if a date and time are more than x hours in the past

I have a table which has a single date column and then a time in and time out column - in some scenarios, it is possible that the time I am needing to check is yesterday and this is where I am struggling.
I can't change the table layout to use date times as this is part of a wider system which I am doing a small amount of work on.
For example, now it is 2019-02-21 20:01:00 and I am looking for all records which have a time in of more than 8 hours ago.
This is the query I am using:
SELECT id from table where
((date='2019-02-21' and time_in < date_sub(now(),interval 8 hour))
or
(date='2019-02-20' and time_in < date_add(now(), interval 16 hour)))
and
time_out='00:00:00'
If I have a date of 2019-02-21 and a time of 10:00 then this works
If I have a date of 2019-02-20 and a time of 10:00 then this works
If I have a date of 2019-02-21 and a time of 23:00 then this does NOT work
Is it possible to get this working with the scenario where the time plus the negative of the interval takes it over midnight/00:00?
Use TIMESTAMP function to combine date and time, then compare with dates:
WHERE TIMESTAMP(date, time_in) < CURRENT_TIMESTAMP - INTERVAL 8 HOUR