event calendar

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vzwhaley
Forum Newbie
Posts: 5
Joined: Tue Oct 18, 2005 10:04 am

event calendar

Post by vzwhaley »

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.

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\">&laquo; Prev</a>\n <span class=\"SectionHeader4\">".$monthName." ".$year."</span> <a href=\"?year=".$y2."&month=".$m2."\" class=\"Author3\">Next &raquo;</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\">&nbsp;</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\">&nbsp;</td>\n";
    }
    $out .= "</tr>\n";
  }
  
  $out .= "</table>\n";
    
    return $out;
} 

// Draw the calendar
echo drawCalendar($month, $year);
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

The problem is that you arrange the events by months. Maybe you should try your query differently.

Code: Select all

SELECT `EventStart`, `EventEnd` FROM `Events` WHERE `EventStart` > $firstDayOfTheMonth OR `EventEnd` < $lastDayOfTheMonth
Select the start and end where the event either ends after this month begins, or starts before this month ends. Then, utilize those values to fit your code.
vzwhaley
Forum Newbie
Posts: 5
Joined: Tue Oct 18, 2005 10:04 am

Post by vzwhaley »

so should I run a function to get the first day of the month and the last day of the month and compare those values in the MySQL statement? thanks for your help.
Post Reply