PHP + MySQL + Repeating Calendar Events - php

I know this has been a question asked many times and I wish there was some solid answers out there, so i'm going to try and ask it and see if we can get a different response.
I realize there are tons of complexity's in dealing with recurring events. But is my understanding right if I were to do something like of this nature:
MySQL column (RRULE)
When a user enters in an event ant says, "This repeats", now enter in this RRULE information Such as, (ie: FREQ=YEARLY;BYMONTH=3;BYDAY=2SU )
So use this method instead of INSERTING hundreds of events of the same thing, just create 1 table to store my repeating events and then add this RRULE column for the event, correct?
Question 2. How do you determine the maximum length of an event if stated, "Repeat this forever" (ie: a birthday).
Question 3. Is there a better way to do this, than what I am thinking?
If you could shed some light on what I am questioning, that would be greatly appreciated. I would like to write a class that handles all the information based on this setup but would like to know if I am on the right track or not.
Please and thank you!

Yes, just enter it once. Calculate the first day for it to run, based off user input (what did they choose? yearly, monthly, etc) when you insert it, and put that in as the next run date. Also store their type choice. Then when you run the task at the specified time, you update the next run date/time based off the stored type choice. In addition you may want another field that indicates how many times to repeat; forever, 4 times etc. Decrement this accordingly when running the task.

Related

Running a function/script per user on the server side

I'm working on a basic lamp(willing to change) website , and I currently need a way to run some function on the server that runs for several hours per user, and every X hours it needs to query the mysql database to see if the value for that user has been updated, if it hasn't it need it to insert a new record in the database...I also should mention that the 'every X hours' can change per user too, and the total runtime of the function per user can also vary.
So basically I need a function that runs continuously on the server for few hours per user. What is the best way to do this? I want the site to be able to support many users (like 10000 +).
I'm willing to try new technologies for every aspect of the site, I'm still in the design phase and I was looking for some input.
I've looked at cron but not really sure how well it would work when dealing with so many users...
edit: Here is a typical scenario of events;
User presses button on the website and closes the browser.
Server starts a timer from when they pressed the button, now
the server will check if that user has pressed a different button within a given time frame (time frame can change per user), say within 30 minutes. If they didn't press the other button then the server needs to automatically insert a new record in the database.
The script will need to continue running, checking every 30 mins for say the next 5 hours.
Thank you!
Cron would work as well as you can code the page it will run. It's not a cron limitation.
The question is ambiguous btw. Maybe explaining your full scenario would help.
Meanwhile, my suggestion would be to set up a scrip that allows you to manually check what you need to check.
You definitely need the DB to be InnoDB optimized with proper indexes to be able to support 1000 plus users.
To alleviate the number of calls to the database, a common practice is to run scripts only on what you are interested (so in the case of users you would only select those who have logged on in say the past 3 hours)
That's achievable in 2 ways, a simple select statement, or by adding entries to a specific table on the login page, and remove them after the automated script has finished running.
All of this is pure theory without understanding exactly what you need to do though.
You are telling what/how you want to do, but not why you want to do it. Maybe letting us know why could lead to a different how ;)
However, what you can do is still use cron (or anything similar). The trick is to have
a last_interaction timestamp column
a maximum_interval column
a daily_runtime column
in your users database. Not optimized but you are in the design phase so you shouldn't pay too much attention to the performance aspect (except is explicitly required).

Checking database for changes to a field over time period

not sure if this is possible to do but...
i have a follow function on my cms and would like a form of 'trending' posts that would allow me to show the posts that have gained the most followers over the past 7 days or so.
If i have a way of counting the amount of followers for today, is there a way i can check it against a past amount?
Or whats the best way around this? should i possibly be creating a script that will run a query on all the blogs, count there followers and store that in a DB field? I would make this happen every 7 days and run a check against this number and then the current follower amount?
Is this possibly over complicating things?
Thanks, Craig.
It looks like you are headed towards a Cron Job that will run periodically
If i have a way of counting the amount of followers for today, is there a way i can check it against a past amount?
Yes, save your data in a table just for this purpose. Then setup a script that checks this special table in your database where the stats are kept, and chooses the top ones. This table get's updated everyday, say at 00:00. With the data saved, you may query it to find out as much as you're saving in it
TLDR: this is kind of a project in itself, but I'm no expert

PHP / MySQL timetable comparison

I have an idea for a project that I am currently working on, I am however struggling to find a way to implement this in PHP/MySQL.
I have a database with a list of activities that a tattoo parlour has, basically he works from 9 to 5 and when an event is added it is added to the database using date and time
| --- DATE----- | TIME |
2012-11-02 ---| 10:00:00
What I am trying to do is list all the times and dates within the tattooist timetable in the following week that does not already have events going on, this will also ensure the tattooist doesn't double book too!
any assistance will help guys, any assistance will help
The best way to create the application is to store each scheduled event into a database and "black out" dates that are already taken by the applications "events" table (per say)... You will probably want to do your event creation checks during the point that the event would be created or written to the mysql db.... You will probably want to have an events table with a start and end time for each event.... when you go to schedule an event (assuming user input dictates event time and date) just write a mysql query that selects the event where the DATE(a.lastevent) = DATE(NOW())... let me know if you have further questions
Edit:
I would build the front end of the application calendar style using a JavaScript interface. In the long run it will be much more user friendly and much easier to create, I've implemented them in a day.
The framework I suggest using is ExtJS, there are many tutorials and extensions for calendars, I know that ExtJS 3 has a calendar built into it and you can view a tutorial on that here: http://www.sencha.com/learn/using-calendar
Additionally, if you decide to use ExtJS4, you can use this plugin: https://github.com/bmoeskau/Extensible/downloads or this one http://www.sencha.com/forum/showthread.php?142488-Extjs-4-based-open-source-Calendar-application
The second link is open source, the demo looks to be Spanish (it does what you need and more), but if you use Chrome you'll get a bar at the top asking if you want the page translated, click yes, it does a great job. From there you can do find and replaces to get it to English.
The ExtJS 3 tutorial will show you how to interface the calendar with a PHP page so that you can update your MySQL database via AJAX. It is much simpler than you might think, and you'll save a ton of time trying to code it from scratch.
Sometimes it depends on how long each scheduled event might be. Are you adding these events with just a timestamp or are you also tracking how long of a block is being assigned?
you could have an appointment set using a start and stop time and some math..
I have a annoying tendency to use a epoch timestamp instead of a mysql date time. then you can make calculations to see if an appointment overlaps or find the gaps. just have to learn to count in seconds or provide math to adjust :).

user generated / user specific functions

I'm looking for the most elegant and secure method to do the following.
I have a calendar, and groups of users.
Users can add events to specific days on the calendar, and specify how long each event lasts for.
I've had a few requests from users to add the ability for them to define that events of a specific length include a break, of a certain amount of time, or require that a specific amount of time be left between events.
For example, if event is >2 hours, include a 20min break. for each event, require 30 minutes before start of next event.
The same group that has asked for an event of >2 hours to include a 20 min break, could also require that an event >3 hours include a 30 minute break.
In the end, what the users are trying to get is an elapsed time excluding breaks calculated for them. Currently I provide them a total elapsed time, but they are looking for a running time.
However, each of these requests is different for each group. Where one group may want a 30 minute break during a 2 hour event, and another may want only 10 minutes for each 3 hour event.
I was kinda thinking I could write the functions into a php file per group, and then include that file and do the calculations via php and then return a calculated total to the user, but something about that doesn't sit right with me.
Another option is to output the groups functions to javascript, and have it run client-side, as I'm already returning the duration of the event, but where the user is part of more than one group with different rules, this seems like it could get rather messy.
I currently store the start and end time in the database, but no 'durations', and I don't think I should be storing the calculated totals in the db, because if a group decides to change their calculations, I'd need to change it throughout the db.
Is there a better way of doing this?
I would just store the variables in mysql, but I don't see how I can then say to mysql to calculate based on those variables.
I'm REALLY lost here. Any suggestions? I'm hoping somebody has done something similar and can provide some insight into the best direction.
If it helps, my table contains
eventid, user, group, startDate, startTime, endDate, endTime, type
The json for the event which I return to the user is
{"eventid":"'.$eventId.'", "user":"'.$userId.'","group":"'.$groupId.'","type":"'.$type.'","startDate":".$startDate.'","startTime":"'.$startTime.'","endDate":"'.$endDate.'","endTime":"'.$endTime.'","durationLength":"'.$duration.'", "durationHrs":"'.$durationHrs.'"}
where for example, duration length is 2.5 and duration hours is 2:30.
Store only the start time and end time for the event, and a BLOB field named notes.
I've worked on several systems that suffered from feature creep of these sorts of requirements until the code and data modeling became nothing but an unmaintainable collection of exception cases. It was a lot of work to add new permutations to the code, and typically these cases were used only once.
If you need enforcement of the rules and conditions described in the notes field, it's actually more cost-effective to hire an event coordinator instead of trying to automate everything in software. A detail-oriented human can adapt to the exception cases much more rapidly than you can adapt the code to handle them.

How to handle dates that repeat indefinitely

I am implementing a fairly simple calendar on a website using PHP and MySQL. I want to be able to handle dates that repeat indefinitely and am not sure of the best way to do it.
For a time limited repeating event it seems to make sense to just add each event within the timeframe into my db table and group them with some form of recursion id.
But when there is no limit to how often the event repeats, is it better to
a) put records in the db for a specific time frame (eg the next 2 years) and then periodically check and add new records as time goes by - The problem with this is that if someone is looking 3 years ahead, the event won't show up
b) not actually have records for each event but instead when i check in my php code for events within a specified time period, calculate wether a repeated event will occur within this time period - The problem with this is that it means there isn't a specific record for each event which i can see being a pain when i then want to associate other info (attendance etc) with that event. It also seems like it might be a bit slow
Has anyone tried either of these methods? If so how did it work out? Or is there some other ingenious crafty method i'm missing?
I'd take approach b and if someone adds something to it, I'd create a "real" event entry.
Edit:
How many periodic events do you expect and what kind of periodic events would that be? (eg: every monday, every two weeks etc.)
I would create a single record for a repeated event. Then in case more info has to be added to a specific date, I would create a record for the attachment with a reference to the repeated event.
Third vote for option B - rationale being that the data should only ever be queried for a limited timeframe (i.e. start and end). For performance reasons I'd suggest that, in addition to storing the date/time of the first occurrence, number of occurrences and frequency that you also maintain the last occurrence in the database.
C.
From my experience, generating recurring dates and checking if a specific date is in that pattern isn't all that bad performance-wise. There's only 365 days in a year. 10,000 days is already almost 30 years. which means, the size of the input/output is relatively small in a practical scenario.
This library may help (but it's javascript): http://github.com/mooman/recurring_dates

Categories