TLDR: Need to understand the best way for a user to be able to add a date(s) & time(s) for an event, and how to structure database
Explanation:
When a user adds an event, they need to be able to choose the date of the event, whether or not it will repeat daily/weekly/monthly, start and end time of the event or "all-day", if it's weekdays only or weekend...etc. Basically everything you can do w/ Google calendar when you create an event and they need to be able to edit it too (if that matters). But ALSO, they need to be able to add another date/time - for instance:
Add an event where on Monday and Wednesday of this week and three weeks from now, it goes from 8-10pm. On Tuesday and Thursday this week only, it goes from 6-9pm.
My thoughts so far:
Create a "dates" table with a HABTM relationship w/ my "events" table. When a user adds a date (with all the options of repeat..etc etc., it runs a function to process those repeats/limits...etc and adds all the dates into the dates table w/ their start/end times.
But - then how do I manage it if they want to edit that - since it just created multiple fields.
Question / Help?:
Am I even on the right track with this? I'm new to CakePHP, and it's hard for me to wrap my head around the best ways to do things... I'm not yet looking for technical help (would not turn it down though) - for now, I just need to get the idea for the best way to structure everything to be able to manage this. Maybe I need a "dates" table AND a "times" table? Maybe a "dates" table with an id that references many individual rows in a "dates_data" table?
Thank you very much ahead of time for any help / direction!
You're doing great. Let me just share my thoughts.
If I would design this, I'd have 3 models:
Event
id
user_id
description
created (datetime)
updated (datetime)
Schedule
id
event_id
description
start (datetime)
end (datetime)
duration (time, if empty(NULL) it means this is a whole day event)
repeat_time (e.g. 3:00pm means 3pm daily)
repeat_day (for weekly/monthly, e.g. Monday, Monday & Tuesday, Monday to Friday)
repeat_date (for monthly, e.g. 1 means every 1st day of month, 31 means every 31st or end of the month)
repeat_anniversary (for specific date every year, e.g. every December 25th)
Date
id
schedule_id
start (datetime)
end (datetime)
Now let's have an example of an event. Let's say we want an event that will repeat every Saturday and Sunday of May & June 2011 at 1:00pm until 3:00pm (two hours):
The events table contains the basic detail of an event. One record will be saved here.
The schedules table is separated so that you could add multiple schedules. One record will also be saved in schedule with the following fields:
duration: 02:00
start: 2011-05-01
end: 2011-06-30
repeat_time: 13:00:00
repeat_day: 01,07 (Sunday & Saturday)
Now on dates table, there will be 17 records, one for each occurrence of the schedule. The reason why I separated this is that it will be easier for me know when will the event fall. This will be useful, for example, when creating the calendar. One of the records for the dates table will look like this:
start: 2011-05-01 13:00:00
end: 2011-05-01 15:00:00
Now what if the user edits the schedule? The schedule record would be edited. All dates record would also be edited. You don't wanna delete and recreate the dates, since you might use each record for another model (e.g. user might want to tag other users as attendees for each date of the event).
I hope this helps. Goodluck on your project!
Related
I'm trying to create a schedule of unique, recurring events that cycle on a weekly basis (each event will have unique identifiers and repeat weekly).
In addition to other information gathered about each event, I will gather the weekday on which each event will take place and the dates (calendar dates) on which the event will begin and end.
An example:
Event: Go to gym (the event I plan to do once a week)
Day of Week: Sunday (day of week event will occur (every Sunday))
Start Date: 2014-09-01 (happens to be a Monday)
End Date: 2015-08-31 (...)
The purpose is to write a script that takes any multitude of events and their respective weekday/start/end date and recreate a calendar depicting the future calendar dates on which each event will occur.
So the first date I should see scheduled to go to the gym will be:
2014-09-07
because this is first Sunday following the start date.
To sum it up, I am gathering the weekday of each event as well as the start and end dates. How can I parse these pieces of data into something that spits out a list of the events' future dates of reoccurrence?
Keep in mind that there will be a vast number of events that will occur on different days of the week and have different start/end dates.
Thank you for all who read and respond.
Tried:
SQL: grouping, different select statements, and stuff
PHP: stuff, date/time stuff, and stuff stuff
For next event in schedule you could use strtotime function:
strtotime('2014-09-01 next Sunday');
and use returned UNIX time.
But for complete solution of your problem there can be many ways. IMHO, strtotime used in for cycle can give you a large overhead if calculated on demand. If you store schedule in DB, perhaps it will be easiest solution to generete dates and regenerate them on edit.
In other hand, dates are pain in head so algoritm to calculate all ocasions can be much bigger. You can use intervals calculated from difference of result date from first date if $firstDate = $startDate or if event happens every day once a week, then just add 7 days to it.
Post more details or your solution to find mistakes.
i'm pretty new to programming and can't seem to figure out my mistake here.
I have a calendar setup, every time a user changes a day I make a new row. This way all changes are logged (again new, id imagine there is a better way.) each row has some displayed information but its primary differentiated by a month a day and a year. IE 1 31 2013 .
I need to get the most recent unique row for each day of the month. So if I run a query for 1/31/2013, I need to return only the most recently created row WHERE month= '1' AND etc.
I'm using..
SELECT t.* FROM(SELECT * FROM calendar a
WHERE NOT EXISTS(SELECT * FROM calendar b
WHERE b.day = a.day AND b.lastaltered > a.lastaltered)) t
WHERE t.month = '12' AND t.year = '2013'
From PHP, if that matters.
Now it works fine if a user makes changes slowly. But I found if someone is quick with it like entering multiple days which end up having a very close time stamp ("lastaltered") it doesn't return that row with my current query. I tested this by modifying the time stamp to a later date and it then returned normally. I hope that explains my problem well enough. I'm still not clear as to why altering the time stamp caused the row to return.
Thank you for your time!
- Jer
Ah ... managing time in a database that doesn't understand what time is (and none of them do).
Go here, read the first book - it explains the intricacies of time and the difficulties of using one data type (DateTime) to represent different concepts:
A fixed instant: 10:15am 4 December 2013 UTC
A recurring instant: 10:15am every day, every Tuesday or the last Monday of a month
An instant defined from an anchor: 2 hours from now
A floating interval: 2 hours
An anchored interval: 2 hours from 10:15am to 12:15pm 4 December 2013 UTC
An instant that does not exist: 2:30 am Sunday 5 October 2014 Australian Eastern Daylight Time
An instant that happens twice 1 hour apart: 2:30am Sunday 6 April 2014 Australian Eastern Daylight Time
... and you get the idea.
The cleanest way to handle your problem is to have a ValidFrom and ValidTo DateTime field, when the user creates the row the ValidFrom is set to now and the ValidTo is set to NULL and a trigger executes that sets all the old entries with a NULL ValidTo to now. This will give a complete audit trail and you can get the current entry by querying for the one with the NULL ValidTo.
We are trying to design a program that will allow users to book tutors based on tutors' availability. So we want tutors to tell us when (what time, day, etc) they are available/not available, and when a user books a specific day and time, we only want to show that user the tutors that are available for that particular day and time.
For example, tutors could tell us they are available M-W from 9-5PM.
How could I design such a table?
I was thinking of doing the followin
non-availability:
(this allows tutors to tell me they are not available on a specific date ... say 4/15/2012 10PM)
date date
time_start datetime
time_end datetime
recurring_non-availability
(this allows tutors to tell me that they are not available every Monday from 9-5pm)
dayOfWeek enum
time_start
time_end
So basically, tutors can specify for each day of the week, the times they are not available. They can also specify specific dates they are not available. If a user wants a tutor for a specific date like 4/20 10AM, I will first query the non-availability table to make sure there are no conflicts for 4/20 10 AM, and then query the recurring_non_availability table to make sure they are no conflicts for Friday (4/20 is on a Friday) 10 AM. I am not sure if this design will give me the best picture of a tutor's availability.
Perhaps set up a table with all the days of the week tutors will be available and then have the times they are stored within like so?
Tutor monday tuesday wednesday thursday friday
bob 9-5 9-5 9-5 10-3 9-12
I'm building a system that shows "events for this month", listed by day and hour.
When I create the event, I set a start date, an end date and a hour.
Let's say that one event starts at 07-10-2011 and ends 07-12-2011. The problem is that some days in this date range will not feature the event. As an example, this event may happen all days and at the same hour, except some few days where it will not happen or has a different hour (think about a show with an opening date different than the rest of the days).
I'm using PHP, MySQL and Codeigniter and my doubt is about the right way to save those dates in the database. Another table with all the dates and the event ID, or save them all in a field inside the event row? Or something else?
Thanks
I'd create two tables. The first table is an events table, and the other is an events_dates table. This way you can create a single event and have as many dates linked to it as you want.
The events_dates table can be as detailed or simple as you want. If it were me, I'd probably have a start_time and end_time column, as well as an event_id and any other data you want.
I would store the range date in a table and then create an "exception" table where you can store your exceptions.
I have started creating a website for a new online radio station, launching later in the year, and so far the basics are going well - it's running on Apache 2.2, with PHP 5.28.
However, I have one problem I'm struggling to find a solution for - how to store day and time in the database my site runs on, not date and time.
This is the type of thing I mean:
http://www.metroradio.co.uk/staff.asp
http://www.radioaire.co.uk/showdj.asp?DJID=41140
Notice in the second example how the date and time is shown:
This is my example:
Monday: 2:00 PM - 6:00 PM
Tuesday, Thursday - Saturday: 10:00 AM - 2:00 PM
Sunday: 7:00 PM - 11:00 PM
Although the links above use ASP.NET and IIS, my site uses PHP, and I'm not sure of the best way to get this to work in PHP.
This is my database structure for the presenter pages:
presenterid - autoincrement, INT 11
presentername - varchar, 255
airtime - DATETIME
showdesc - varchar 255
Anyone got a good solution?
thanx
You could create a second table, with the following fields :
show_id INT (foreign key)
day_id INT (use date('w'), date('S') or your own system)
begin TIME (time at which the show begins)
end TIME (time at which the show ends)
You must also add an unique constraint for the couple (show_id,day_id).
When you want to fetch the show, a simple JOIN will give you the times you want.
You could have a program table (presenter, description, etc.), having a one-to-many relationship to a schedule table (day, start time, end time).
Then, you just have to query for all the schedules of a given program, ordered by day, and detect similar start/end times for consecutive days to present them on one line.