I want to organize some kinds of actions in different periods of time, for example i want to make some UPDATES in certain days (the end of the month, the beginning of the next month, one specific day at one specific hour)...
In my example i have to follow some records that have int variable and i want to turn their values to 0 ad a certain time.
I've tried to use the sleep function but it loops over and over and i think this is not a usable solution...
Any solution?
Thanks
You probably need to read about cronjob, the below link should resolve your problem:
http://www.thegeekstuff.com/2011/07/php-cron-job/
This is an interesting link as well : http://net.tutsplus.com/tutorials/php/managing-cron-jobs-with-php-2/
in case you are using windows you can take a look at this : Use php to set cron jobs in Windows
Related
I'm working with icalendar events that make use of RRULE to deal with repetitions;
Now, i'm aware that the are some php class like When and RRules etc.. to handle RRULE and i'm already using it, to accomplish the task of generating repeating events, but the problem is about performance with long date-range.
So i thought to speed up the task of generating repetitions by limiting the range ( start & end ) by current calendar views that are [ MONTH, WEEK, DAY ].
Assuming we have a repeating event like
FREQ=DAILY;INTERVAL=1;DTSTART:2009-01-01
what i do is obviously to change the DTSTART to today date and to add an UNTIL date to limit the loop to a close range, and it is working just fine. the problem comes with rules like these:
FREQ=WEEKLY;BYDAY=SU;DTSTART:2009-01-01
or
FREQ=WEEKLY;INTERVAL=5;DTSTART:2009-01-01
with this kind of rules my trick doesn't work because the original start date doesn't match my harcoded today date.
i have tried without luck to make some iteration using DateTime Period and Interval but i can't figure it out.
So what i'm asking is a way to find when a given date will recur in my view range that can be MONTH, WEEK, or single DAY.
thanks in advance hope someone can help me. ;)
I know the question is quite old already, but I'm going to answer just in case.
There is no reliable way to alter the rule like you are trying to do. As you noticed, as soon as you start having anything more than just the basic daily/weekly/monthly/yearly repetition your trick doesn't work anymore. And you haven't even scratched the surface yet, things like BYSETPOS and COUNT are a real nightmare.
You only have two approaches:
Either loop through all occurrences, starting from DTSTART ignoring anything that is before the start of your period, and stopping once you reach the end of the period
Or generate the full list of dates of your period, and test each one against the RRULE. This can be done with a process of elimination (example: your RRULE only occurs on Sunday and the date is not a Sunday? Then discard and move on). However in the most complex cases, the only solution is to revert to option 1 and compute all the occurrences.
While you can code these yourself, I suggest to use a lib for that. I'm the author of php-rrule and with the lib you can use getOccurrencesBetween($begin, $end) (that implements option 1) and occursAt($date) (that implements option 2).
First of all I am a beginner, and I don't want anybody to write code for me. I would just like a bit of a hint from a more experienced developer.
I have a video site, what loads videos from another website with XML and saves info about the videos in the database. What I would like to do is that if a week is passed, automatically run the insert query.
I never did this before and never worked with time functions like this. So please could someone show his plan how he would do it? So no code, just explain the process.
I'd recommend setting up a cron: http://en.wikipedia.org/wiki/Cron
I dont think this is a coding-related problem. Tasking can be achieved by using cron.
Cron is a task scheduler, which when its available for your hosting, can be accessed at the hosting control panel. What is your host ?
You could use Cronjobs.
What do you want to do with the data in the week in between? I hope you're not hoping to keep a process running for a week and then execute the insert.
You could do something like load the XML and save it in the database, setting an active column to 0. You save the timestamp at the moment the insert is executed.
Meanwhile, using cron, you let a script run every X minutes or hours, checking the database for items that have been inactive for a week, and then updating them to become active.
You can use the time() function. It returns the number of seconds since January, 1st, 1970.
Then, for example, you take the time at t = 0.
When time() - t > a week (= 3600 * 24 * 7 seconds), you know a week has passed by.
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.
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
Example
7.30pm, second Monday each month
or
7.30pm, first & third Thursday each month.
Basically I want a upcoming events list for the next month.
How do I handle reoccurring events in PHP/MySQL?
Assuming a linux like environment: Have a cron job execute your php script.
I think cron has a pretty flexible way of recording schedules for repeating tasks, it might be worth diving into how it works (it is open source).
I also found this class for parsing crontab entries - http://www.phpclasses.org/browse/package/2568.html - I can't vouch for how good the code is, it's quite old (but crontab doesn't change much). The example given uses a MySQL database to store the tasks, so it might be worth at least looking at for ideas.