event calendar
Posted: Mon Jun 04, 2007 7:36 am
Hello everyone and thanks much for your past help.
I have created a community events calendar and am trying to program the calendar to enable links on days that there are events listed in our MySQL table titled Events. Two of the fields in the Events table are titled EventStart and EventEnd and I have a function that will successfully return a range of all the days in a month that have events and will enable an active link on those days in the calendar.
My problem is that if you have an event that starts, say on May 31 and goes through June 2, May 31 will have an active link, but when you navigate to the month of June, the 1 and 2 are not enabled as active links.
I realize this has to do with the EventStart and EventEnd in the MySQL statement, but I am trying to determine how I can return a range of dates for each month that actually have events listed in the Events table.
Any help would be appreciated.
I have created a community events calendar and am trying to program the calendar to enable links on days that there are events listed in our MySQL table titled Events. Two of the fields in the Events table are titled EventStart and EventEnd and I have a function that will successfully return a range of all the days in a month that have events and will enable an active link on those days in the calendar.
My problem is that if you have an event that starts, say on May 31 and goes through June 2, May 31 will have an active link, but when you navigate to the month of June, the 1 and 2 are not enabled as active links.
I realize this has to do with the EventStart and EventEnd in the MySQL statement, but I am trying to determine how I can return a range of dates for each month that actually have events listed in the Events table.
Any help would be appreciated.
Code: Select all
// Check to see if any of the variables are set in the query string, and if not, default them to today
$year = isset($_GET['year']) ? $_GET['year'] : date('Y');
$month = isset($_GET['month']) ? $_GET['month'] : date('m');
// Connect to MySQL
mysql_select_db($database_JCPress, $JCPress);
function getEventDays($month, $year) {
$days = array();
$sql = "SELECT EventStart, EventEnd FROM Events WHERE MONTH(EventStart) = '$month' AND YEAR(EventStart) = '$year'";
$Recordset1 = mysql_query($sql);
if (mysql_num_rows($Recordset1) > 0) {
while ($row = mysql_fetch_assoc($Recordset1)) {
$startStamp = strtotime($row['EventStart']);
$endStamp = strtotime($row['EventEnd']);
$eventLength = ($endStamp - $startStamp) / (24 *3600);
for($i = 0; $i <= $eventLength; $i++) {
$results = date('d', strtotime("+$i day", $startStamp));
$days[] = $results;
}
}
}
return $days;
}
function drawCalendar($month, $year) {
// set variables we will need to help with layouts
$first = mktime(0,0,0,$month,1,$year); // timestamp for first of the month
$offset = date('w', $first); // what day of the week we start counting on
$daysInMonth = date('t', $first);
$monthName = date('F', $first);
$StoryMain = array('Su', 'M', 'Tu', 'W', 'Th', 'F', 'Sa');
$eventDays = getEventDays($month, $year);
foreach($eventDays as $Day) {
echo $Day. "<br>";
}
// Previous month link
$prevTS = strtotime("$year-$month-01 -1 month"); // timestamp of the first of last month
$pMax = date('t', $prevTS);
$pDay = ($day > $pMax) ? $pMax : $day;
list($y, $m) = explode('-', date('Y-m', $prevTS));
// Next month link
$nextTS = strtotime("$year-$month-01 +1 month");
$nMax = date('t', $nextTS);
$nDay = ($day > $nMax) ? $nMax : $day;
list($y2, $m2) = explode('-', date('Y-m', $nextTS));
// Start drawing calendar
$out = "<table cellpadding=\"2\">\n";
$out .= "<tr><th colspan=\"7\"><a href=\"?year=".$y."&month=".$m."\" class=\"Author3\">« Prev</a>\n <span class=\"SectionHeader4\">".$monthName." ".$year."</span> <a href=\"?year=".$y2."&month=".$m2."\" class=\"Author3\">Next »</a>\n</th></tr>\n";
$out .= "<tr align=\"center\" valign=\"middle\" bgcolor=\"#666666\">\n";
foreach ($StoryMain as $wd) {
$out .= "<td class=\"StoryMain\">".$wd."</td>\n";
}
$i = 0;
for ($d = (1 - $offset); $d <= $daysInMonth; $d++) {
if ($i % 7 == 0) {
$out .= "<tr align=\"center\" valign=\"middle\" bgcolor=\"#CCCCCC\">\n"; // Start new row
}
if ($d < 1) {
$out .= "<td class=\"nonMonthDay\"> </td>\n";
} else {
if (in_array($d, $eventDays)) {
if ($d == date("d") && $month == date("m")) {
$out .= "<td class=\"StoryMain\" bgcolor=\"#FFFFCC\">\n";
$out .= "<a href=\"http://www.johnsoncitypress.com/New/EventList6.php?Year=".$year."&Month=".$month."&Day=".$d."\" class=\"Author3\">".$d."</a>\n";
$out .= "</td>\n";
} else {
$out .= "<td class=\"StoryMain\">\n";
$out .= "<a href=\"http://www.johnsoncitypress.com/New/EventList6.php?Year=".$year."&Month=".$month."&Day=".$d."\" class=\"Author3\">".$d."</a>\n";
$out .= "</td>\n";
}
} else {
if ($d == date("d") && $month == date("m")) {
$out .= "<td class=\"StoryMain\" bgcolor=\"#FFFFCC\">".$d."</td>\n";
} else {
$out .= "<td class=\"StoryMain\">".$d."</td>\n";
}
}
}
$i++; // Increment position counter
if ($i % 7 == 0) {
$out .= "</tr>\n"; // End row on the 7th day
}
}
// Round out last row if we don't have a full week
if ($i % 7 != 0) {
for ($j = 0; $j < (7 - ($i % 7)); $j++) {
$out .= "<td class=\"nonMonthDay\"> </td>\n";
}
$out .= "</tr>\n";
}
$out .= "</table>\n";
return $out;
}
// Draw the calendar
echo drawCalendar($month, $year);