I'm trying to write a function to move an scheduled task. The schedule can not overlap with any other event. My user inputs are as follows:
schedule_id (int)
new_start_time (DATETIME)
My table structure is as follows:
Schedules
| schedule_id | start_time | end_time | task_id
| 1 | 2015-12-21 02:00:00 | 2015-12-21 04:00:00 | 1
| 2 | 2015-12-21 08:30:00 | 2015-12-21 09:30:00 | 1
| 3 | 2015-12-22 01:00:00 | 2015-12-22 02:00:00 | 2
Tasks
| task_id | name | max_duration
| 1 | do things | 2
| 2 | do stuff | 1
A user has between start_time and end_time to start a "task". The user can not begin the "task" until that window. Once that user begins the task they have whatever the max_duration for that task ID is to complete it. There is also a 15 minute window to set up for the next task. That means a user who starts a task 1 second before the end of the window still has max_duration amount of time to complete the task. Therefore the "actual window" that nothing can be scheduled in is start_time to (end_time+max_duration+15). I would like to move an event (or insert a new one) but I must check for overlaps. Essentially I must ensure:
Does the start_time from user input run into any other schedule's end_time+max_duration+15?
Does the end_time+max_duration+15 run into any other schedule's start time. end_time is simply obtained by taking the new start_time and adding the original duration (end_time = (orig_end_time-orig_start_time)+start_time
For example, the above table is valid for schedule_id's 1 and 2 because a user can start any time between 2:00 and 4:00. Assuming he starts right at the end, 3:59:59 the event will last at max until 5:59:59. Even with the cleanup window of 15 minutes this still leads to 6:14:59 and since the next schedule starts at 8:30 this is ok.
I've been wrapping my head around this for hours. I would like to do it in pure MySQL however I am considering using PHP if I really have to. Even in PHP this problem seems difficult. Sure I could grab every schedule with a start time a day or two earlier and an end time a day or two later then compare my interval but that seems very hacky.
Any ideas?
Related
I want to automatically reset the value in hours_remaining to its default value according to year. I also want the year to auto-incremented. Can anyone give me some advise or help on how to implement this? By the way I'm new to using Php MySql.
From this:
employee_id | year | default_value | hours_remaining |
========================================================
1 | 2022 | 80 | 67 | <---- When current year
To this when another year:
employee_id | year | default_value | hours_remaining |
========================================================
1 | 2023 | 80 | 80 | <---- When year is changed
This is the perfect use of MySQL's EVENT, which can perform periodic jobs. Just make sure the event scheduler is toggled on. Then create a event and watch it happens:
delimiter //
set global event_scheduler=on //
drop event if exists newyear_update //
create event newyear_update on schedule every 1 year starts '2023-01-01' do
BEGIN
update testtb set year=year+1,hours_remaining=default_value ;
END//
Of course you are not going to wait another 8 months to find out. To test it, you can just change the time to every 60 second starts now()
So I have a quiz with a count down, Each question takes 15 seconds to answer.
When the user starts a question, I save the question_id + the end_time which would be after 15 seconds from now.
The table looks like that:
quiz_attempts
________________________________________
| id | user_id | question_id | end_time |
|____|_________|_____________|__________|
| 1 | 1 | 1 | end_time |
|____|_________|_____________|__________|
Then when the page is fully loaded, I show a counter for the user which starts from 15 seconds and ends with 0. And the user has to answer the question within 15 seconds.
<span class="counter">15</span>
The issue is that the end_time would be sooner than the 15 seconds counter shown to the user.
So for example the end_time is 12:06:22 and the counter is shown to the user ends 12:06:25. And it would depend on the internet speed of the user.
So how to time both times so that the end_time ends with the front-end counter like 12:06:22 for both?
I don't want to start with the end_time from the database, As I want the page loaded 1st so that the user can start reading and answering the question within 15 seconds not less
This is the table:
+----+-----------+------------+----------+
| id | id_sensor | start_time | end_time |
+----+-----------+------------+----------+
| 1 | 12 | 21:15:00 | 02:45:00 |
| 2 | 7 | 00:00:00 | 23:15:00 |
| 3 | 5 | 04:30:00 | 16:30:00 |
+----+-----------+------------+----------+
I need to get record(s) where a specific time (e.g. 01:00:00) passed by PHP is between. start_time and end_time are TIME fields in UTC, I'm passing to the query hour via php, note, converted in php from user_timezone to UTC.
SELECT * FROM test WHERE TIME('01:00:00') BETWEEN start_time AND end_time;
Query returns only record id 2, not the 1. I need both, in this case (for id 1, end time ofcourse is next day).
Of course, if we looking for TIME('01:00:00'), we don't need the id 3.
Thank you.
I think this is the logic you want:
SELECT *
FROM test
WHERE (start_time < end_time AND TIME('01:00:00') BETWEEN start_time AND end_time) OR
(start_time > end_time AND TIME('01:00:00') NOT BETWEEN end_time AND start_time);
now() returns both, current time and date. You can also work with curtime(), which returns only the current time.
BTW, i think that working with SELECT * should be avoided (maybe you used it just for this example), it is IMHO always better to list the fields needed explicitly.
We have developed a website for pharmacies in which we show the opened pharmacies in a specific city.
I handle this with a field named "timestamps" in wich it is stored all timestamps of opening hour every 15 minutes for a period of about 3 months.
For example if every days the farmacy is open from 8:00 to 19:00 there is a range of timestamps from one time to another, with an interval of 15 minutes.
While in the frontend we have a list of opened pharmacies and I could show opened ones by querying the database with for example:
"WHERE timestamps LIKE('%1449820800%')" where the timestamp is the current time rounded to the nearest quarter hour.
The question is: considering the time ranges are different from week to week, is there a better way to handle this situation? Also because we have 25.000 users and the website is slow if we have large amount of timestamps.
Thank you in advance!
You could just have a database with each day of openning for every store :
-----------------------------------------
| StoreId | Day | HourOpen | HourClose |
=========================================
| 1 | 1 | 8:30 | 21:15 |
-----------------------------------------
| 1 | 2 | 9:00 | 17:00 |
-----------------------------------------
| 2 | 1 | 10:00 | 12:30 |
-----------------------------------------
| 2 | 1 | 14:00 | 19:00 |
=========================================
In this table, the day represent the day of the week (1 for monday, 2 for tuesday for example) and then you just have to parameter the openniong hours for each store only once.
You can then query this table to see if a store is open for a day of the week at the very moment.
If a pharmacy has an exceptionnal closure or openning hours for a day, i suggest an ovveride table like this one
----------------------------------------------------------
| StoreId | Date | isOpen | HourOpen | HourClose |
==========================================================
| 1 | 2015-12-20 | true | 10:00 | 16:00 |
----------------------------------------------------------
| 2 | 2015-12-20 | false | null | null |
==========================================================
This way, you can check first if an pharmacy has any record in this table for the current day (not depending of the time) if it does, you check if there is an opening. If there is not any entry in the override table, you check with the first table.
You also can ahve a hour model table with opening and closing time, a day table, and an associative table that creates relations between stores, hours and days.
I am currently working on a project where i was required to develop a timeslot system where employee can occupy a time slot within a specific day. I am using military time as my time format 00:00:00 - 23:00:00 and the days are from sunday - saturday. I just want them to book times for each day.
what is the correct mysql schema for this?
How do i go about handling validation, making sure that they only book for the available timeslots.
----------------------------------------------------
USER_ID | DAY | STARTS_AT | ENDS_AT
----------------------------------------------------
| | |
| | |
| | |
| | |
| | |
------------------------------------------------------
Well I think the logic of which slot is available should be in your code, and then you simply keep record of which employee booked what time slot, using mysql datetime, don't think you need an extra day field, maybe something like id, user_id, start_time, end_time