PHP Event Calendar - Design advice
Posted: Fri Oct 06, 2006 11:41 am
I am building a small Event Calendar for use with a Google Maps application I am working on... I need some advice on how I should go about drawing and populating the calendar. As of right now, all the markup of how to draw the table is hard-coded into the draw() method. Is there even a practical way around this? The biggest question is really how to go about populating this calendar with data received from a database... this is what it looks like right now:
Thanks for looking at it. I really appreciate it!
Code: Select all
<?php
class MC2_Calendar{
protected $week = array(
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday'
);
protected $month;
public function __construct(MC2_Calendar_Month $month){
$this->month = $month;
}
public function draw(){
$output = "<table border='1' cellspacing='0' cellpadding='0' class='MC2_Calendar' id='MC2_Calendar'>\n";
$output .= " <tr>\n <td colspan='" . count($this->week) . " class='MC2_Calendar_Header'><span class='MC2_Calendar_Title'>" . $this->month->fullName() . "</span> <span class='MC2_Calendar_Year'>" . $this->month->year() . "</span></td>\n </tr>\n <tr>\n";
foreach($this->week as $day){
$output .= " <th class='MC2_Calendar_Heading'>" . $day . "</th>\n";
}
$output .= "</tr><tr>\n";
$first_day = date('w', $this->month->date());
for ($i=0; $i<$first_day; $i++){
$output .= "<td> </td>\n";
}
for ($list_day=1; $list_day <= $this->month->totalDays(); $list_day++){
$output .= "<td><span class='MC2_Calendar_day_number'>" . $list_day . "</span></td>\n";
if ($first_day == 6){
$output .= "</tr>\n";
$output .= "<tr>\n";
$first_day = -1;
}
$first_day++;
$days_left = ($first_day) ? 7 - $first_day : 0;
// Print out the week's remaining day's cells
if($list_day == $this->month->totalDays()){
for($i = 1; $i <= $days_left; $i++){
$output .= "<td> </td>\n";
}
}
}
$output .= "</tr>\n";
$output .= "</table>\n";
echo $output;
}
}
class MC2_Calendar_Month_Exception extends Exception{
}
class MC2_Calendar_Month{
/**
* Numerical representation of day
* @integer
*/
//protected $day;
/**
* Numerical representation of month
* @integer
*/
protected $month;
/**
* Numerical representation of year
* @integer
*/
protected $year;
/**
* Timestamp to extract date info from
* @integer
*/
protected $date;
public function __construct($month, $year){
//$this->day = (integer) $day;
$this->month = (integer) $month;
$this->year = (integer) $year;
$this->setDate();
}
protected function setDate(){
$this->date = mktime(0,0,0,$this->month, 1, $this->year);
}
public function totalDays(){
$days = date('t', $this->date);
return (integer) $days;
}
public function startDay(){
return (string) date('l', $this->date);
}
public function fullName(){
return (string) date('F', $this->date);
}
public function year(){
return $this->year;
}
public function month(){
return $this->month;
}
public function date(){
return $this->date;
}
}
$show_month = isset($_GET['month']) ? $_GET['month'] : date('n', time());
$show_year = isset($_GET['year']) ? $_GET['year'] : date('Y', time());
if(($show_month+1) > 12){
$next_month = 1;
$next_year = $show_year + 1;
}
else{
$next_month = $show_month +1;
$next_year = $show_year;
}
if(($show_month - 1) < 1){
$prev_month = 12;
$prev_year = $show_year - 1;
}
else{
$prev_month = $show_month - 1;
$prev_year = $show_year;
}
$links = "<a href='?month=" . $prev_month . "&year=" . $prev_year . "'>< Previous</a>  <a href='?month=" . $next_month . "&year=" . $next_year . "'>Next ></a>";
echo $links;
$month = new MC2_Calendar_Month($show_month, $show_year);
$calendar = new MC2_Calendar($month);
$calendar->draw();