PHP Code:
class Generic_calendar {
var $_date;
var $_highlight;
var $_start_secs;
var $_total_days;
var $_month;
var $_month_text;
var $_year;
function Generic_calendar($date=0) {
$this->_date =&$date;
$this->_highlight = array();
$this->_start_secs = 0;
$this->_total_days = 0;
$this->_month = 0;
$this->_month_text = '';
$this->_year = 0;
}
function highlight($days=array()) {
$this->_highlight =&$days;
}
function getPrevMonth() {
return strtotime('-1 month', $this->_date);
}
function getNextMonth() {
return strtotime('+1 month', $this->_date);
}
function getMonth() {
if ($this->_month==0)
$this->_month = date('n', $this->_date);
return $this->_month;
}
function getMonthText() {
if ($this->_month_text=='')
$this->_month_text = date('M', $this->_date);
return $this->_month_text;
}
function getYear() {
if ($this->year==0)
$this->_year = date('Y', $this->_date);
return $this->_year;
}
function getUnixStart() {
if ($this->_start_secs==0)
$this->_start_secs = mktime(0, 0, 0, $this->getMonth(), 0, $this->getYear());
return $this->_start_secs;
}
function getTotalDays() {
if ($this->_total_days==0)
$this->_total_days = date('t', $this->_date);
return $this->_total_days; ;
}
function getDays() {
$start_secs = $this->getUnixStart();
$padding = date('w', $start_secs)+1;
$total_days = date('t', $this->_date);
$days = array();
$offset = 8 - $padding;
$padding_end = 7 - $padding;
if ($padding<7)
while ($padding--)
$days[] = array( 'day' => 0);
for ($x=1; $x<=$total_days; $x++) {
$days[] = array(
'day' => $x,
'selected' => in_array( $x, $this->_highlight),
'new_week' => $new_week = ($x==$offset) && ($offset += 7),
'timestamp' => $start_secs,
);
$start_secs += 86400;
}
while ($padding_end--)
$days[] = array( 'day' => 0);
return $days;
}
function getDaysOfWeek($offset=0) {
return array(
0 + $offset => 'Sun',
1 + $offset => 'Mon',
2 + $offset => 'Tues',
3 + $offset => 'Wed',
4 + $offset => 'Thur',
5 + $offset => 'Fri',
6 + $offset => 'Sat',
);
}
}
example usage:
PHP Code:
function dispCalendar($date, $days=array()) {
$cal = new Generic_calendar($date);
$cal->highlight($days);
$days = $cal->getDays();
//print_r($days);
$next_month = $cal->getNextMonth();
$prev_month = $cal->getPrevMonth();
?>
<table id="calendar">
<tr><td><a href="?date=<?php echo $prev_month; ?>"><</a></td>
<td colspan="5"><?php echo $cal->getMonthText().' '.$cal->getYear(); ?></td>
<td><a href="?date=<?php echo $next_month; ?>">></a></td></tr>
<tr>
<?php
foreach ($days as $day) {
if ($day['new_week'] && $day['day']>1)
print '</tr><tr>';
$class = $day['selected'] ? 'selected' : 'normal';
print '<td class="'.$class.'">';
if ($day['day']==0) {
print ' ';
} else if ($day['selected']) {
print '<a href="?date='.$day['timestamp'].'">'.$day['day'].'</a>';
} else {
print $day['day'];
}
print '</td>';
}
print '</tr></table>';
}
?>
<style type="text/css"><!--
td { vertical-align: top; }
#calendar { text-align: center; font-size: 8pt; }
#calendar .normal { }
#calendar .selected { color: #ff0000; }
#calendar a { color: #0000FF; }
--></style>
<?php
if ($_GET['date']) {
$date = (int)$_GET['date'];
} else {
$date = time();
}
dispCalendar($date, array(5,6,7));
?>
I like it when people tear my code apart if only to make me better.. so, feel free (and I know I should be commenting.. and I think the 'algorithm' I'm using for figuring out the start and end padding of a month is probably not so good).