Advertisement

Author Topic: php recurring events  (Read 2604 times)

0 Members and 1 Guest are viewing this topic.

Offline darkhelmet

  • Moderator
  • *****
  • Posts: 1557
  • Respect: +56
    • View Profile
php recurring events
« on: August 26, 2010, 10:53:30 AM »
Hi,
I'm starting to work on a php based calendar (events) and I would like to implement a recurring events option.

Does anyone have any snippet or class that can calculate the dates if I provide the starting date, end date and occurrence? e.g. daily, weekly (s, m, t, w, th, f, s), monthly, annually.

Also, if you have any suggestion on sql/db setup, I'd appreciate your input.
I was thinking of doing the straight forward approach - every event having their own record.
So I'd set up two tables, one to store all the event records and one to store info on recurring events.
That way each record can be edited and old events are still accessible.


Thanks!



Like this post: 0
I made you read this.

Adverstisement

jetter

  • Guest
Re: php recurring events
« Reply #1 on: August 26, 2010, 04:19:42 PM »
it could be done either in PHP or in SQL.

in php:

Code: [Select]
$date = date("Y-m-d");// current date

$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +30 days");


in SQL,
Code: [Select]
DATE_ADD(startDate, INTERVAL period periodType)

DATE_SUB(startDate, INTERVAL period periodType)

If I was to implement this; my table would look like this:


EVENT
-------------
uid
name
description
lft
rgt

EVENTDATE_REL
--------------
eventuid
eventdateuid

EVENTDATE
-------------
uid
day
month
year
hour

RECURRING_EVEN TDATE_REL
-----------------------
eventdateuid
recurringuid


RECURRING
-------------
uid
day
month
year
hour


This will allow me to have Events that have the ability to have multiple childrens events and each of those (parent and childrens) events could use the same event date setting and recurring time frame.




« Last Edit: August 26, 2010, 04:21:51 PM by jetter »

Like this post: 0

Offline darkhelmet

  • Moderator
  • *****
  • Posts: 1557
  • Respect: +56
    • View Profile
Re: php recurring events
« Reply #2 on: August 29, 2010, 04:16:26 PM »
jetter,
i came up with this function for the day of the week range.  is there a better way?

Code: [Select]
$sd = "08/01/2010";  //start date
$ed = "08/31/2010";  //end date
$thedays = "1,5";  //the days, e.g. sun=0, mon=1, tue=2 ...

echo dayoftheweek($sd,$ed,$thedays);

function dayoftheweek($sd,$ed,$thedays){
$start = strtotime($sd);
$end = strtotime($ed);
$calcdate = strtotime("sunday", $start);
while($calcdate <= $end) {
for ($x=0;$x<=6;$x++){
if ((in_array($x,explode(',',$thedays)))and($calcdate<=$end)){
$output .= date("D n/j/Y", strtotime("+$x days", $calcdate)) . "<br/>";
$calcdate = strtotime("+1 weeks", $calcdate);
}
}
}
return $output;
}


« Last Edit: August 29, 2010, 04:38:36 PM by darkhelmet »

Like this post: 0
I made you read this.

Offline darkhelmet

  • Moderator
  • *****
  • Posts: 1557
  • Respect: +56
    • View Profile
Re: php recurring events
« Reply #3 on: August 30, 2010, 02:58:06 PM »
I just did some testing on my initial code above and it doesn't work.  It's skipping to the next week once a date is found.
I'm redoing it based on an open source program I'm dissecting from.

Code: [Select]
$valid = true;                        
for($x=0;$x < $frequency*7;$x++){
$occurance = date('Y-m-d  H:i:s', mktime(date('H', strtotime($startDate)), date('i', strtotime($startDate)), 0, date('m', strtotime($startDate)) , date('d', strtotime($startDate))+(($x*7)*$interval), date('y', strtotime($startDate)) ) );

$lastweek=sprintf("%02d", (strftime('%W',strtotime($occurance))-0));
$year = strftime('%Y',strtotime($occurance));
for ($i=0;$i<=6;$i++){
$thisDOW = strftime('%w',strtotime("+{$i} day",strtotime($occurance)));
    $occDate = strftime('%Y-%m-%d', strtotime("+{$i} day",strtotime($occurance)));
   
   //-- Check if the date is one of the assigned and less than the end date
   if(in_array($thisDOW, $onwd) && strtotime($occDate) <= strtotime($endDate)){
$ar_Recur[] = $occDate;
   }
   
   //-- If the date is past the end date end the loop
   if(strtotime($occDate) >= strtotime($endDate)){
$valid = false; //-- End the loop
break;
   }
}
if(!$valid) break;
   }
echo implode('<br/>', $ar_Recur);


« Last Edit: August 31, 2010, 07:57:21 AM by darkhelmet »

Like this post: 0
I made you read this.

Offline darkhelmet

  • Moderator
  • *****
  • Posts: 1557
  • Respect: +56
    • View Profile
Re: php recurring events
« Reply #4 on: August 31, 2010, 11:21:48 PM »
Thanks for the suggestion.  I'm trying to keep my tables as few as possible since the calendar is just one component of the whole project.
I decided to just use one table for the calendar.

My table fields are like this:

CALENDAR_TBL
//the general fields below

id
name
address
city
state
zip
phone
website
description
contact_firstn ame
contact_lastna me
contact_email
start_datetime
end_datetime

//the recurring fields below

type - Type of recurring event (daily, weekly, monthly, etc...)
dates - Recurring dates (dates that repeat)
countdates - Number of days total (total # days)
days - The days by week (weekly days that repeat... sun, mon, tues...etc...)


If I was to implement this; my table would look like this:


EVENT
-------------
uid
name
description
lft
rgt

EVENTDATE_REL
--------------
eventuid
eventdateuid

EVENTDATE
-------------
uid
day
month
year
hour

RECURRING_EVEN TDATE_REL
-----------------------
eventdateuid
recurringuid


RECURRING
-------------
uid
day
month
year
hour


This will allow me to have Events that have the ability to have multiple childrens events and each of those (parent and childrens) events could use the same event date setting and recurring time frame.






Like this post: 0
I made you read this.

Offline darkhelmet

  • Moderator
  • *****
  • Posts: 1557
  • Respect: +56
    • View Profile
Re: php recurring events
« Reply #5 on: September 01, 2010, 12:22:14 PM »
yeah, i'm finally finished with the calendar!  ;D
turned out pretty well.



Like this post: 0
I made you read this.

jbutton

  • Guest
Re: php recurring events
« Reply #6 on: September 01, 2010, 02:05:56 PM »
Hi,
I'm starting to work on a php based calendar (events) and I would like to implement a recurring events option.

Does anyone have any snippet or class that can calculate the dates if I provide the starting date, end date and occurrence? e.g. daily, weekly (s, m, t, w, th, f, s), monthly, annually.

Also, if you have any suggestion on sql/db setup, I'd appreciate your input.
I was thinking of doing the straight forward approach - every event having their own record.
So I'd set up two tables, one to store all the event records and one to store info on recurring events.
That way each record can be edited and old events are still accessible.


Thanks!

I probably have one single table.

Events Table
Name
Description
StartTime
Endtime
Reccurrence



Like this post: 0

 

Advertisements