简体   繁体   中英

What is the best way to compare time periods and how do I structure my database for it?

I'm looking for a way, that if my app says "Looking for person available from March 3rd, 2016 8:00 am to March 3rd, 2016 3:00 pm " to match it with a person that might be available from March 3rd, 2016 7:00 am to March 3rd, 2016 16:00 pm.

Now this is a just a snapshot, it has to scale for infinite times/dates for example a person might be available from March 3rd, 2016 7:00 am to 9:00 am, then again from 2:00pm to 8:00pm , so I'd have to have a way to filter people like this as a not complete match.

What I have so far is ONE TABLE that holds the dates to match against, so for example March 3rd 8 - 3pm, March 4th 8 - 3pm, March 5th 8 - 3pm..etc..

Another table that holds people and their availability as child elements, but I am not sure how to structure this part to make it easy and efficient to match inside loops with the other table.

Right now, in my employee table I have one field "March 3rd" and then another field with hours/minutes available as an array so for example "8:00, 8:15, 8:30, 8:45, 9:00...etc... The hours increments have to be in 15 min periods, so I can't just put 8am - 5pm for example...especially if the person is available during certain periods of the day...

Here is what my "employee select your availability looks like"

 <table class="table table-bordered table-hover employe-availability-table"> <thead> <tr> <th class="warning"> Hour </th> <th class="warning"> 15 </th> <th class="warning"> 30 </th> <th class="warning"> 45 </th> <th class="warning"> Hour </th> <th class="warning"> 15 </th> <th class="warning"> 30 </th> <th class="warning"> 45 </th> </tr> </thead> <tbody> <tr> <td data-hour="0" data-minute="00" data-day-period="N" class="hour"> 0:00 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="0:00" style="display: inline-block;"></td> <td data-hour-ref="0" data-minute="15" data-day-period="N"> 15 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="0:15" style="display: inline-block;"></td> <td data-hour-ref="0" data-minute="30" data-day-period="N"> 30 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="0:30" style="display: inline-block;"></td> <td data-hour-ref="0" data-minute="45" data-day-period="N"> 45 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="0:45" style="display: inline-block;"></td> <td data-hour="12" data-minute="00" data-day-period="J" class="hour"> 12:00 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="12:00" style="display: inline-block;"></td> <td data-hour-ref="12" data-minute="15" data-day-period="J"> 15 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="12:15" style="display: inline-block;"></td> <td data-hour-ref="12" data-minute="30" data-day-period="J"> 30 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="12:30" style="display: inline-block;"></td> <td data-hour-ref="12" data-minute="45" data-day-period="J"> 45 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="12:45" style="display: inline-block;"></td> </tr> <tr> <td data-hour="1" data-minute="00" data-day-period="N" class="hour"> 1:00 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="1:00" style="display: inline-block;"></td> <td data-hour-ref="1" data-minute="15" data-day-period="N"> 15 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="1:15" style="display: inline-block;"></td> <td data-hour-ref="1" data-minute="30" data-day-period="N"> 30 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="1:30" style="display: inline-block;"></td> <td data-hour-ref="1" data-minute="45" data-day-period="N"> 45 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="1:45" style="display: inline-block;"></td> <td data-hour="13" data-minute="00" data-day-period="J" class="hour"> 13:00 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="13:00" style="display: inline-block;"></td> <td data-hour-ref="13" data-minute="15" data-day-period="J"> 15 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="13:15" style="display: inline-block;"></td> <td data-hour-ref="13" data-minute="30" data-day-period="J"> 30 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="13:30" style="display: inline-block;"></td> <td data-hour-ref="13" data-minute="45" data-day-period="J"> 45 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="13:45" style="display: inline-block;"></td> </tr> <tr> <td data-hour="2" data-minute="00" data-day-period="N" class="hour"> 2:00 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="2:00" style="display: inline-block;"></td> <td data-hour-ref="2" data-minute="15" data-day-period="N"> 15 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="2:15" style="display: inline-block;"></td> <td data-hour-ref="2" data-minute="30" data-day-period="N"> 30 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="2:30" style="display: inline-block;"></td> <td data-hour-ref="2" data-minute="45" data-day-period="N"> 45 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="2:45" style="display: inline-block;"></td> <td data-hour="14" data-minute="00" data-day-period="J" class="hour"> 14:00 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="14:00" style="display: inline-block;"></td> <td data-hour-ref="14" data-minute="15" data-day-period="J"> 15 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="14:15" style="display: inline-block;"></td> <td data-hour-ref="14" data-minute="30" data-day-period="J"> 30 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="14:30" style="display: inline-block;"></td> <td data-hour-ref="14" data-minute="45" data-day-period="J"> 45 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="14:45" style="display: inline-block;"></td> </tr> <tr> <td data-hour="3" data-minute="00" data-day-period="N" class="hour"> 3:00 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="3:00" style="display: inline-block;"></td> <td data-hour-ref="3" data-minute="15" data-day-period="N"> 15 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="3:15" style="display: inline-block;"></td> <td data-hour-ref="3" data-minute="30" data-day-period="N"> 30 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="3:30" style="display: inline-block;"></td> <td data-hour-ref="3" data-minute="45" data-day-period="N"> 45 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="3:45" style="display: inline-block;"></td> <td data-hour="15" data-minute="00" data-day-period="S" class="hour"> 15:00 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="15:00" style="display: inline-block;"></td> <td data-hour-ref="15" data-minute="15" data-day-period="S"> 15 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="15:15" style="display: inline-block;"></td> <td data-hour-ref="15" data-minute="30" data-day-period="S"> 30 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="15:30" style="display: inline-block;"></td> <td data-hour-ref="15" data-minute="45" data-day-period="S"> 45 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="15:45" style="display: inline-block;"></td> </tr> <tr> <td data-hour="4" data-minute="00" data-day-period="N" class="hour"> 4:00 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="4:00" style="display: inline-block;"></td> <td data-hour-ref="4" data-minute="15" data-day-period="N"> 15 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="4:15" style="display: inline-block;"></td> <td data-hour-ref="4" data-minute="30" data-day-period="N"> 30 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="4:30" style="display: inline-block;"></td> <td data-hour-ref="4" data-minute="45" data-day-period="N"> 45 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="4:45" style="display: inline-block;"></td> <td data-hour="16" data-minute="00" data-day-period="S" class="hour"> 16:00 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="16:00" style="display: inline-block;"></td> <td data-hour-ref="16" data-minute="15" data-day-period="S"> 15 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="16:15" style="display: inline-block;"></td> <td data-hour-ref="16" data-minute="30" data-day-period="S"> 30 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="16:30" style="display: inline-block;"></td> <td data-hour-ref="16" data-minute="45" data-day-period="S"> 45 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="16:45" style="display: inline-block;"></td> </tr> <tr> <td data-hour="5" data-minute="00" data-day-period="N" class="hour"> 5:00 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="5:00" style="display: inline-block;"></td> <td data-hour-ref="5" data-minute="15" data-day-period="N"> 15 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="5:15" style="display: inline-block;"></td> <td data-hour-ref="5" data-minute="30" data-day-period="N"> 30 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="5:30" style="display: inline-block;"></td> <td data-hour-ref="5" data-minute="45" data-day-period="N"> 45 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="5:45" style="display: inline-block;"></td> <td data-hour="17" data-minute="00" data-day-period="S" class="hour"> 17:00 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="17:00" style="display: inline-block;"></td> <td data-hour-ref="17" data-minute="15" data-day-period="S"> 15 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="17:15" style="display: inline-block;"></td> <td data-hour-ref="17" data-minute="30" data-day-period="S"> 30 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="17:30" style="display: inline-block;"></td> <td data-hour-ref="17" data-minute="45" data-day-period="S"> 45 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="17:45" style="display: inline-block;"></td> </tr> <tr> <td data-hour="6" data-minute="00" data-day-period="N" class="hour"> 6:00 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="6:00" style="display: inline-block;"></td> <td data-hour-ref="6" data-minute="15" data-day-period="N"> 15 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="6:15" style="display: inline-block;"></td> <td data-hour-ref="6" data-minute="30" data-day-period="N"> 30 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="6:30" style="display: inline-block;"></td> <td data-hour-ref="6" data-minute="45" data-day-period="N"> 45 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="6:45" style="display: inline-block;"></td> <td data-hour="18" data-minute="00" data-day-period="S" class="hour"> 18:00 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="18:00" style="display: inline-block;"></td> <td data-hour-ref="18" data-minute="15" data-day-period="S"> 15 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="18:15" style="display: inline-block;"></td> <td data-hour-ref="18" data-minute="30" data-day-period="S"> 30 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="18:30" style="display: inline-block;"></td> <td data-hour-ref="18" data-minute="45" data-day-period="S"> 45 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="18:45" style="display: inline-block;"></td> </tr> <tr> <td data-hour="7" data-minute="00" data-day-period="J" class="hour"> 7:00 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="7:00" style="display: inline-block;"></td> <td data-hour-ref="7" data-minute="15" data-day-period="J"> 15 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="7:15" style="display: inline-block;"></td> <td data-hour-ref="7" data-minute="30" data-day-period="J"> 30 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="7:30" style="display: inline-block;"></td> <td data-hour-ref="7" data-minute="45" data-day-period="J"> 45 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="7:45" style="display: inline-block;"></td> <td data-hour="19" data-minute="00" data-day-period="S" class="hour"> 19:00 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="19:00" style="display: inline-block;"></td> <td data-hour-ref="19" data-minute="15" data-day-period="S"> 15 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="19:15" style="display: inline-block;"></td> <td data-hour-ref="19" data-minute="30" data-day-period="S"> 30 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="19:30" style="display: inline-block;"></td> <td data-hour-ref="19" data-minute="45" data-day-period="S"> 45 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="19:45" style="display: inline-block;"></td> </tr> <tr> <td data-hour="8" data-minute="00" data-day-period="J" class="hour"> 8:00 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="8:00" style="display: inline-block;"></td> <td data-hour-ref="8" data-minute="15" data-day-period="J"> 15 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="8:15" style="display: inline-block;"></td> <td data-hour-ref="8" data-minute="30" data-day-period="J"> 30 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="8:30" style="display: inline-block;"></td> <td data-hour-ref="8" data-minute="45" data-day-period="J"> 45 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="8:45" style="display: inline-block;"></td> <td data-hour="20" data-minute="00" data-day-period="S" class="hour"> 20:00 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="20:00" style="display: inline-block;"></td> <td data-hour-ref="20" data-minute="15" data-day-period="S"> 15 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="20:15" style="display: inline-block;"></td> <td data-hour-ref="20" data-minute="30" data-day-period="S"> 30 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="20:30" style="display: inline-block;"></td> <td data-hour-ref="20" data-minute="45" data-day-period="S"> 45 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="20:45" style="display: inline-block;"></td> </tr> <tr> <td data-hour="9" data-minute="00" data-day-period="J" class="hour"> 9:00 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="9:00" style="display: inline-block;"></td> <td data-hour-ref="9" data-minute="15" data-day-period="J"> 15 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="9:15" style="display: inline-block;"></td> <td data-hour-ref="9" data-minute="30" data-day-period="J"> 30 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="9:30" style="display: inline-block;"></td> <td data-hour-ref="9" data-minute="45" data-day-period="J"> 45 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="9:45" style="display: inline-block;"></td> <td data-hour="21" data-minute="00" data-day-period="S" class="hour"> 21:00 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="21:00" style="display: inline-block;"></td> <td data-hour-ref="21" data-minute="15" data-day-period="S"> 15 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="21:15" style="display: inline-block;"></td> <td data-hour-ref="21" data-minute="30" data-day-period="S"> 30 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="21:30" style="display: inline-block;"></td> <td data-hour-ref="21" data-minute="45" data-day-period="S"> 45 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="21:45" style="display: inline-block;"></td> </tr> <tr> <td data-hour="10" data-minute="00" data-day-period="J" class="hour"> 10:00 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="10:00" style="display: inline-block;"></td> <td data-hour-ref="10" data-minute="15" data-day-period="J"> 15 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="10:15" style="display: inline-block;"></td> <td data-hour-ref="10" data-minute="30" data-day-period="J"> 30 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="10:30" style="display: inline-block;"></td> <td data-hour-ref="10" data-minute="45" data-day-period="J"> 45 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="10:45" style="display: inline-block;"></td> <td data-hour="22" data-minute="00" data-day-period="S" class="hour"> 22:00 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="22:00" style="display: inline-block;"></td> <td data-hour-ref="22" data-minute="15" data-day-period="S"> 15 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="22:15" style="display: inline-block;"></td> <td data-hour-ref="22" data-minute="30" data-day-period="S"> 30 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="22:30" style="display: inline-block;"></td> <td data-hour-ref="22" data-minute="45" data-day-period="S"> 45 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="22:45" style="display: inline-block;"></td> </tr> <tr> <td data-hour="11" data-minute="00" data-day-period="J" class="hour"> 11:00 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="11:00" style="display: inline-block;"></td> <td data-hour-ref="11" data-minute="15" data-day-period="J"> 15 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="11:15" style="display: inline-block;"></td> <td data-hour-ref="11" data-minute="30" data-day-period="J"> 30 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="11:30" style="display: inline-block;"></td> <td data-hour-ref="11" data-minute="45" data-day-period="J"> 45 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="11:45" style="display: inline-block;"></td> <td data-hour="23" data-minute="00" data-day-period="N" class="hour"> 23:00 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="23:00" style="display: inline-block;"></td> <td data-hour-ref="23" data-minute="15" data-day-period="N"> 15 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="23:15" style="display: inline-block;"></td> <td data-hour-ref="23" data-minute="30" data-day-period="N"> 30 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="23:30" style="display: inline-block;"></td> <td data-hour-ref="23" data-minute="45" data-day-period="N"> 45 <input type="checkbox" class="hide undoUniform" name="field_108890_tab_14" value="23:45" style="display: inline-block;"></td> </tr> </tbody> </table> 

Any nudge in the right direction regarding my logic and setup would be greatly appreciated.

Did a quick ideone for it here SQLFiddle always messes up for me.

Whether this is the best solution or not, time will tell; but I do believe it is a solution to your problem.

We have your Employee Table, and we also have an Availability Table

The Availability table will contain a start and an end and anything in between those two values will be considered the duration / available time

(I don't have time right now to make fancy class diagrams and such I'm afraid, so you'll have to deal with text :p!)

To clarify the tables:

Employee
@id
name
...

Availability
@id
start
end
user_id

If you filled these in with values such as:

INSERT INTO Employee
    (`id`, `name`)
VALUES
    (1, 'Mike');

INSERT INTO Availability
  (`id`, `start`, `end`, `user_id`)
VALUES
  (1, '2016-01-01 12:00:00', '2016-01-01 17:00:00', 1),
  (2, '2016-01-01 14:00:00', '2016-01-01 19:00:00', 1); 

Then we could use the following query to get all available entries that are available at a certain time:

SELECT * 
FROM Availability
WHERE '2016-01-01 18:00:00' AND '2016-01-01 18:30:00'
BETWEEN start AND end;

The above yields:
2 | 2016-01-01 14:00:00 | 2016-01-01 19:00:00 | 1

If you then wanted to get the Employee, you could LEFT JOIN based on the user_id to get the rest of the details you want.

I believe that should solve your requirement. Any issues let me know.

Edit: Not sure what your requirement is afterwards, but just as a slight added part:

If you pull it out from the database, I would suggest you use PHP's DateTime library for parsing it.

$date = DateTime::createFromFormat('Ymd H:i:s', $row['start']);

Then when you present the dates back to the user you can make it human readable:

$date->format('d/m/YH:i:s');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM