Created
January 23, 2009 00:36
-
-
Save sunny/50813 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Calendar class to print out an HTML calendar. | |
* | |
* Usage: | |
* $calendar = new Calendar(); | |
* $calendar->addEvent(2009, 8, 13, '/foo'); | |
* $calendar->addEvent(2009, 8, 28, '/bar', 'After the foo comes the bar'); | |
* $calendar->printMonth(2009, 8); | |
*/ | |
class CalendarDay { | |
function __construct($stamp, $event = null) { | |
$this->stamp = $stamp; | |
$this->event = $event; | |
} | |
function isToday() { | |
return date('Y-m-d', $this->stamp) == date('Y-m-d'); | |
} | |
function isMonth($m) { | |
return intval($m) == intval(date('n', $this->stamp)); | |
} | |
function date($format) { | |
return date($format, $this->stamp); | |
} | |
} | |
class Calendar { | |
function __construct() {} | |
function addEvent($year, $month, $day, $uri, $title = '') { | |
$date = $this->ansi_mktime($year, $month, $day); | |
$this->events[date('Y-m-d', $date)] = array('uri' => $uri, 'title' => $title); | |
} | |
function eventFor($date) { | |
return $this->events[date('Y-m-d', $date)]; | |
} | |
function ansi_mktime($year, $month = 1, $day = 1, $hour = 0, $minute = 0, $second = 0) { | |
return mktime($hour, $minute, $second, $month, $day, $year); | |
} | |
// Return array of weeks in a month, starting on monday from the month before | |
// finishing on sunday from the month after | |
// Weeks are arrays of timestamps. | |
function weeksForMonth($year, $month) { | |
$one_day = 24*60*60; | |
$first_day_of_the_month = $this->ansi_mktime($year, $month, 1); | |
$day_stamp = $this->ansi_mktime($year, $month, | |
2 - intval(date('N', $first_day_of_the_month))); | |
$next_month = date('n', $this->ansi_mktime($year, $month+1)); | |
$weeks = array(); | |
// while we are in the same month | |
while ($cur_month = date('n', $day_stamp) != $next_month) { | |
$week = array(); | |
// make arrays of 7 days | |
for ($i = 0; $i < 7; ++$i) { | |
$week[]= new CalendarDay($day_stamp, $this->eventFor($day_stamp)); | |
$day_stamp += $one_day; | |
} | |
$weeks[] = $week; | |
} | |
return $weeks; | |
} | |
function printMonth($year, $month) { | |
?> | |
<table> | |
<thead> | |
<tr> | |
<th><abbr title="Lundi">L</abbr></th> | |
<th><abbr title="Mardi">M</abbr></th> | |
<th><abbr title="Mercredi">M</abbr></th> | |
<th><abbr title="Jeudi">J</abbr></th> | |
<th><abbr title="Vendredi">V</abbr></th> | |
<th><abbr title="Samedi">S</abbr></th> | |
<th><abbr title="Dimanche">D</abbr></th> | |
</tr> | |
</thead> | |
<tbody> | |
<?php foreach ($this->weeksForMonth($year, $month) as $days) : ?> | |
<tr> | |
<?php foreach ($days as $day) : ?> | |
<td class="<?php if ($day->isToday()) echo 'today'; ?> <?php if ($day->isMonth($month)) echo 'thismonth'; ?>"> | |
<?php if ($day->event) :?> | |
<a href="<?php echo htmlspecialchars($day->event['uri']) ?>" | |
title="<?php echo htmlspecialchars($day->event['title']) ?>"> | |
<?php echo $day->date('d') ?> | |
</a> | |
<?php else : ?> | |
<?php echo $day->date('d') ?> | |
<?php endif; ?> | |
</td> | |
<?php endforeach; ?> | |
</tr> | |
<?php endforeach; ?> | |
</tbody> | |
</table> | |
<?php | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment