Best way to store weekly event in MySQL?

冷暖自知 提交于 2019-12-03 13:05:24

Here's a straightforward way:

EventID Title    Mon Tue Wed Thu Fri Sat Sun BeginningDate EndDate
1       MyEvent  0   0   0   1   0   0   0   14-01-2010    14-01-2033

How to use:

Simply set a 1 on the days you want to run it. Since the 7-days calendar is not likely to change any time soon, that structure should be immutable. You can choose any combination of days.

To recap:

Run every Thursdays:

EventID Title    Mon Tue Wed Thu Fri Sat Sun BeginningDate EndDate
1       MyEvent  0   0   0   1   0   0   0   14-01-2010    14-01-2033

Run every Thursdays & Mondays:

EventID Title    Mon Tue Wed Thu Fri Sat Sun BeginningDate EndDate
1       MyEvent  1   0   0   1   0   0   0   14-01-2010    14-01-2033

Further more, you get only one row per event schedule, which is easier and cleaner to handle programmatically.

For example, to find all events to be executed on monday, do:

select * from Events where Mon = 1

Can you add an DayOfWeek column in your table and make it an int? Valid values for that would be 1 thru 7. You could add a constraint on that to enforce that rule. For time, how about a BeginTime columns and an EndTime column? They would be int's as well 0-24

For an event at 5:00 pm on Monday would look like this in your table

Event_ID DayOfWeek BeginTime EndTime
1        2         1700      1800

Why not have several lines, each line having only one column containing the day of week. This column would be just a simple :

ENUM("Monday", "Tuesday", ...)

Then, in PHP you could use date and strtotime functions to get the name of the day :

echo date('l');
echo date('l', strtotime('mon'));
// Outputs "Monday"

It is way more readable to store the name of the day.

In case anyone coming this way again I am using SMTWHFA quite efectively with a simple string search.

So Monday Wednesday Friday would be MWF and Sunday Monday Thursday would be SMH. Takes a while to get used to H=Thursday (second letter) and Saturday=A but it works!

Since there won't be any new days of the week invented (I hope!) you could just create a bool column for each day. Then, if you are running a query to find events on a Friday, it would simply be (with a bit of pseudocode):

SELECT eventName
FROM events
WHERE fridayBool = true
AND eventStartTime < NOW()
AND eventEndTime > NOW();

In that example the name of the column you would store in an array in your code, and check today's date to see what day of the week it is, which then selects the proper name out of the array before creating the query.

Not the most elegant, but it should work.

Edit...

Example table columns:

  • eventID
  • eventName
  • eventStartTime
  • eventEndTime
  • sundayBool
  • mondayBool
  • ...
  • saturdayBool

Break the weekly information into a separate table entirely:

events: id, beginTime, endTime, name, description
eventsbyday: id, dayofweek, event_id

This will allow you to query eventsbyday according to the current day of the week:

SELECT events.name, events.beginTime, events.endTime FROM eventsbyday JOIN events ON eventsbyday.event_id=events.id WHERE dayofweek=0

This is very rough code, but the idea is to break out the weekly information, allowing you to have the same event associated with multiple days of the week.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!