Incorporate a recuring schedule into calendar

hi there! want to incorporate a recuring schedule into the PHP calendar below. eg highlight every other weekend, every third Tuesday, every Monday, etc…

on the user side (when they set-up) I’d like to present them with a sample week, Sun thru Sat. then within each day they could specify “every, every other, every third, every fourth”, along with a “beginning when” for each. that should be enough info to pull this off right? surely this has been done before, and don’t want to reinvent the wheel. just asking for a reality check, and to be pointed in the right direction here.

off to my son’s “poetry slam” now, so many thanks in advance!


// gather info to draw calendar
$todays_date = date(“j”);
$todays_month = date(“n”);
$days_in_month = date (“t”, mktime(0,0,0,$_GET[‘month’],0,$_GET[‘year’]));
$first_day_of_month = date (“w”, mktime(0,0,0,$_GET[‘month’]-1,1,$_GET[‘year’]));
$first_day_of_month = $first_day_of_month + 1;
$count_boxes = 0;
$days_so_far = 0;

// fill before-month void of calendar
for ($i = 1; $i <= $first_day_of_month-1; $i++)
{
$days_so_far = $days_so_far + 1;
$count_boxes = $count_boxes + 1;
echo "<td width=\“100\” height=\“100\” class=\“beforedayboxes\”></td>
";
}

// process all active days of the month
for ($i = 1; $i <= $days_in_month; $i++)
{
$days_so_far = $days_so_far + 1;
$count_boxes = $count_boxes + 1;

// this bit checks to see if current box is today’s date,
// if so, it gets highlighted
IF ($_GET[‘month’] == $todays_month+1)
{
IF($i == $todays_date)
{$class = “highlighteddayboxes”;}
ELSE
{$class = “dayboxes”;}
}
ELSE
{
IF($i == 1)
{$class = “highlighteddayboxes”;}
ELSE
{$class = “dayboxes”;}
}

	$link_month = $_GET['month'] - 1;		

// write start of box


HERE I CHECK THE SCHEDULE AND HIGHLIGHT IN ACCORD *


// write end of box
echo "</td>
";

// after 7 boxes start a new row
IF(($count_boxes == 7) AND ($days_so_far != (($first_day_of_month-1) + $days_in_month)))
{
$count_boxes = 0;
echo "</TR><TR valign=\“top\”>
";
}
}

// fill after-month void of calendar
$extra_boxes = 7 - $count_boxes;
for ($i = 1; $i <= $extra_boxes; $i++)
{
echo "<td width=\“100\” height=\“100\” class=\“afterdayboxes\”></td>
";
}

	?&gt;

THANKS AGAIN !!

surely this has been done before

I believe Google Calendar has something approaching this.

If you think about it, you also have to ask how many weeks do you want to book this event into.

And my experience is that they want to do it for the next ten weeks, but not the 7th week, oh yes, and on the 8th week it starts an hour earlier etc.

I am about to embark on something similar, and I think what want to do is not trivial.

great…
mine doesn’t have to be so flexible.
anyone else want to chime in?

thanks CUPS. I’ll post again here when I find a starting point.

Sorry if I am hogging your post, but I just remembered this old discussion about how to handle diary entries, where I again raise the point about recurring events, might be of some use to you (Crikey, 18mths ago, and I’m only starting it next week).

Then there was this thread which contained the line:

“Turning into a good thread this and defintely something to think about further. Hopefully it will give users searching sitepoint for this type of thing a starting point to think about when it comes to events”

They both deal a lot with the question of how you are going to store the data in your database.

There is not a lot of discussion on this topic afaict, but reading those two will at least give you something to go at, that 2nd link contains a link to Fowlers Temporal Expression, and you might also be interested in the PEAR date/date time packages.

Perhaps your application is much simpler and will not need to address these thorny issues, but they might make an interesting read all the same.

HTH and I will shut up now.

thanks for the post Cups, will have a look!

Thanks again for the post Cups. Had a look. See you guys are operating at a depth WAY BEYOND mine. I am VERY NEW and cannot begin to grasp any of that. Fortunately my needs are much simpler so I hope I’m not in over my head. My schedule can be very simple and rigid. All I need is an interval/ frequency for each weekday (every, every other, every third, every fourth). Before I dive in half-cocked I’d like to hear your/someone’s/ anyone’s thoughts.

I’m thinking if I can get some absolute fixed point in the past to call zero, I could set everything else relative to that point and render the schedule that way. I want to generate schedule on the fly as calendar runs.

I’ll make a table holding each weekday [0-6] with the first occurrence of the rule [expressed as some “absolute date”], along with the frequency rule for that weekday [7, 14, 21, or 28].

Set-up:
User is setting up Sunday [0]. I grab TODAY’S “absolute date” – say that’s the zero mentioned above plus 733,650 = $set_up_date. Now, somehow, I determine the next Sunday is 3 days out, so $set_up_date + 3 = $next_one. User wants this schedule to begin 2 Sundays out, so $next_one + 7 = $first_hit. User wants every fourth Sunday scheduled, so I set $frequency to 28. Now the array for Sunday would be [0] [$first_hit] [$frequency]. Repeat this for all other days of the week [1-6].

Now run the calendar: It runs a few days that have already past, of course.
Get “absolute date” for the date being processed = “CALENDAR’S absolute date”.

Select * from table where $frequency=28
IF results
WHILE results
IF $first_hit minus “CALENDAR’S absolute date” = zero, OR divides by 28 evenly without a remainder, we have a hit.
ELSEIF
Select * from table where $frequency=21
IF results
WHILE results
IF $first_hit minus “CALENDAR’S absolute date” = zero, OR divides by 21 evenly without a remainder, we have a hit.
ELSEIF
Select * from table where $frequency=14
IF results
WHILE results
IF $first_hit minus “CALENDAR’S absolute date” = zero, OR divides by 14 evenly without a remainder, we have a hit.
ELSEIF
Select * from table where $frequency=7
IF results
WHILE results
IF $first_hit minus “CALENDAR’S absolute date” = zero OR divides by 7 evenly without a remainder, we have a hit.

Like I said Cups, I AM VERY GREEN. I have yet to figure out how to query the array properly for this job. I have no clue if any “absolute date” is supported in PHP or SQL. AND I don’t know how to see if the difference between 2 dates divides evenly without a remainder. BUT I DO think the heart of this code should work: by ELSEIF-ing through the bigger divisors first, I eliminate the math problem of everything that’s divisible by 28 is also divisible by 14 and 7. Hence I eliminate the problem of “hitting” on every Sunday when I only want every fourth (per example).

I see leap-year should be a problem, but once I figure out the other things this approach should work for me… yes? If so, any tips on “absolute date”, looking for remainders, and plowing through that array would be greatly appreciated !!

Thanks again!