erjiang
erjiang

Reputation: 45667

Best way to store weekly event in MySQL?

I have a table of weekly events that run on certain days of the week (e.g. MTWTh, MWF, etc.) and run on a certain time (e.g. 8am-5pm). What's the best way to store day of week information in MySQL to make retrieving and working with the data easiest? My CakePHP app is going to need to retrieve all events happening NOW().

For time of day, I would just use TIME. For days of the week, I had considered a 7-bit bitfield, a varchar ("MTWThFr" type deal) for the days of the week, but both of those seem like clunky solutions (the varchar being clunkier).

Any suggestions?

Upvotes: 9

Views: 6541

Answers (6)

BeNice
BeNice

Reputation: 2305

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!

Upvotes: 1

Wadih M.
Wadih M.

Reputation: 13462

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

Upvotes: 12

jshalvi
jshalvi

Reputation: 72

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.

Upvotes: 0

mexique1
mexique1

Reputation: 1693

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.

Upvotes: 1

JYelton
JYelton

Reputation: 36512

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

Upvotes: 0

Billy Coover
Billy Coover

Reputation: 3837

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

Upvotes: 4

Related Questions