PebHmong Discussion Forum
Hobbies & Interests => Computer & Technology => Topic started by: darkhelmet 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!
-
it could be done either in PHP or in SQL.
in php:
$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,
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.
-
jetter,
i came up with this function for the day of the week range. is there a better way?
$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;
}
-
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.
$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);
-
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.
-
yeah, i'm finally finished with the calendar! ;D
turned out pretty well.
-
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