<!--PHP script originally from pixelcode.co.uk - Thank you, pixelcode! Modified by Jena R. Hanes.-->
<?php
class Calendar {
var $events;
public function Calendar($date) {
if(empty($date)) $date = time();
define('NUM_OF_DAYS', date('t',$date));
define('CURRENT_DAY', date('j',$date));
define('CURRENT_MONTH_A', date('F',$date));
define('CURRENT_MONTH_N', date('n',$date));
define('CURRENT_YEAR', date('Y',$date));
define('START_DAY', (int) date('N', mktime(0,0,0,CURRENT_MONTH_N,1, CURRENT_YEAR)));
define('COLUMNS', 7);
define('PREV_MONTH', $this->prev_month());
define('NEXT_MONTH', $this->next_month());
$this->events = array();
}
public function prev_month() {
return mktime(0,0,0,
(CURRENT_MONTH_N == 1 ? 12 : CURRENT_MONTH_N - 1),
(checkdate((CURRENT_MONTH_N == 1 ? 12 : CURRENT_MONTH_N - 1), CURRENT_DAY, (CURRENT_MONTH_N == 1 ? CURRENT_YEAR - 1 : CURRENT_YEAR)) ? CURRENT_DAY : 1),
(CURRENT_MONTH_N == 1 ? CURRENT_YEAR - 1 : CURRENT_YEAR));
}
public function next_month() {
return mktime(0,0,0,
(CURRENT_MONTH_N == 12 ? 1 : CURRENT_MONTH_N + 1),
(checkdate((CURRENT_MONTH_N == 12 ? 1 : CURRENT_MONTH_N + 1) , CURRENT_DAY ,(CURRENT_MONTH_N == 12 ? CURRENT_YEAR + 1 : CURRENT_YEAR)) ? CURRENT_DAY : 1),
(CURRENT_MONTH_N == 12 ? CURRENT_YEAR + 1 : CURRENT_YEAR));
}
public function getEvent($timestamp) {
$event = NULL;
if(array_key_exists($timestamp, $this->events))
$event = $this->events[$timestamp];
return $event;
}
//I've modified the addEvent function so that it includes the additional parameters of time, location, and details. Without these variables included in the array_push, only the event title or $event displays when there is more than one listing on a given day.//
public function addEvent($event, $time, $location, $details, $day = CURRENT_DAY, $month = CURRENT_MONTH_N, $year = CURRENT_YEAR) {
$timestamp = mktime(0, 0, 0, $month, $day, $year);
if(array_key_exists($timestamp, $this->events))
array_push($this->events[$timestamp], $event, $time, $location, $details, $day, $month, $year);
else
$this->events[$timestamp] = array($event, $time, $location, $details, $day, $month, $year);
}
//I've modified the makeEvents function so that lists the additional parameters with a line break in between each array value.//
public function makeEvents() {
if($events = $this->getEvent(mktime(0, 0, 0, CURRENT_MONTH_N, CURRENT_DAY, CURRENT_YEAR))) {
$firsts = array_slice($events, 0, 4);
$seconds = array_slice($events, 7, 4);
foreach($firsts as $event) echo $event . '<br />';
foreach($seconds as $event) echo $event . '<br />';
}
}
//I've modified the makeCalendar function so that the calendar starts with Sunday instead of Monday.//
public function makeCalendar() {
echo '<table border="1" cellspacing="4"><tr>';
echo '<td width="90"><a href="?date='.PREV_MONTH.'"><<</a></td>';
echo '<td colspan="5" style="text-align:center">'.CURRENT_MONTH_A .' - '. CURRENT_YEAR.'</td>';
echo '<td width="90"><a href="?date='.NEXT_MONTH.'">>></a></td>';
echo '</tr><tr>';
echo '<td width="90">Sun</td>';
echo '<td width="90">Mon</td>';
echo '<td width="90">Tue</td>';
echo '<td width="90">Wed</td>';
echo '<td width="90">Thu</td>';
echo '<td width="90">Fri</td>';
echo '<td width="90">Sat</td>';
echo '</tr><tr>';
if(START_DAY == 7) {
echo '</tr>';
} else {
echo str_repeat('<td> </td>', START_DAY);
}
$rows = 1;
for($i = 1; $i <= NUM_OF_DAYS; $i++) {
if($i == CURRENT_DAY) {
echo '<td height="45" style="background-color: #C0C0C0"><strong>'.$i.'</strong></td>';
} else if($event = $this->getEvent(mktime(0, 0, 0, CURRENT_MONTH_N, $i, CURRENT_YEAR))) {
echo '<td height="45" style="background-color: #99CCFF"><a href="?date='.mktime(0,0,0,CURRENT_MONTH_N,$i,CURRENT_YEAR).'">'.$i.'</a></td>';
} else {
echo '<td height="45" ><a href="?date='.mktime(0 ,0 ,0, CURRENT_MONTH_N, $i, CURRENT_YEAR).'">'.$i.'</a></td>';
}
if((($i + START_DAY) % COLUMNS) == 0 && $i != NUM_OF_DAYS) {
echo '</tr><tr>';
$rows++;
}
}
echo str_repeat('<td height="45" > </td>', (COLUMNS * $rows) - (NUM_OF_DAYS + START_DAY)).'</tr></table>';
}
//I've created a function to list the upcoming events on the website home page. If there are no events in the coming week, it will print the date and a message.
public function makeThisWeek() {
for($i = CURRENT_DAY; $i <= CURRENT_DAY + 7; $i++) {
if($event = $this->getEvent(mktime(0, 0, 0, CURRENT_MONTH_N, $i, CURRENT_YEAR))) {
echo '<li>' . $event[5] . '/' . $event[4] . ': ' . $event[0] . '</li>';
}
}
}
}
$cal = new Calendar($_GET['date']);
//This is where the different events would be added. I've put these in a separate file so that the data can be accessed by the file index.php which pulls the events within the current 7-day timespan and lists them on the home page.//
require 'events.php';
?>