title: Finding the next time a task should be run given user-defined schedules stored in a database
body: <p>I have an application where a person is allowed to set alarm for particular events for particular time and days. For eg:-
  there are two events- A &amp; B and these events occur repeatedly on certain days of the week schedule of these events go like:</p>
  
  <p>EVENT A - occurs every SUNDAY,MONDAY and WEDNESDAY at 7am and 9pm.<br>
  EVENT B - occurs every SUNDAY and TUESDAY at 6am and 8pm.<br>
  EVENT C - occurs every MONDAY,WEDNESDAY and THURSDAY at 8am,9pm.</p>
  
  <p>The user is shown a form where he can select the days and enter the timings which are same for all the days selected.He can create as many events as he wants.</p>
  
  <p>In the DB as of now I have created 7 columns for each of the days and one column for the timings. I know I might be making it unnecesarily complicated. The above data is represented as </p>
  
  <pre><code>EventNAME SUN MON TUE WED THU FRI SAT Timings
   A         1  1    0   1  0    0   0   7,21
   B         1  0    1   0  0    0   0   6,20
   C         0  1    0   1  1    0   0   8,21
  </code></pre>
  
  <p>Now for some reason I have to find the next nearest day and time when an event occurs For eg: if today is wednesday morning 6am , I have to get the next nearest event as event a -morning 7 am. and at wednesday morning 8 am, I have to get the next nearest event as event C at morning 8 am. </p>
  
  <p>How do I get the next nearest day and time from such a schema? Is there a better way of designing the table?</p>