<?php
class CalendarForm {
// Год
private $year;
// Месяц
private $month;
// День
private $day;
// Признак выходного дня
private $weekend;
// Флажок текущей даты в календаре
private $currentdate;
// Число дней в месяце
private $dayofmonth;
// Счётчик для дней месяца
// В конструкторе - 1-е число(!)
private $day_count;
// Неделя
private $num;
// Массив недель
private $week = array();
// Строка с выводимым кодом html
private $retunrhtml="";
// Конструктор
function __construct($year, $month) {
$this->makeWeeks($year, $month);
}
// Установить год и месяц
public function setYearMonth($year, $month) {
$this->year = $year;
$this->month = $month;
}
// Сбросить счетчик к-ва дней
private function resetDayCount() {
$this->day_count = 1;
}
// Сбросить счетчик недель
private function setFirstWeek() {
$this->num = 0;
}
// Определить к-во дней в месяце
public function getDayOfMonth($year, $month) {
$this->resetDayCount();
return date('t', mktime(0, 0, 0, $month, $this->day_count, $year ));
}
// Установить к-во дней в месяце
private function setDayOfMonth($year, $month) {
$this->dayofmonth = $this->getDayOfMonth($year, $month);
}
// Вычисляем номер дня недели для числа
private function getDayOfWeek() {
return date('w', mktime(0, 0, 0, $this->month, $this->day_count, $this->year ));
}
// Вычисляет следующий месяц от текущего календарного
public function getNextMonth() {
return date('m', mktime(0, 0, 0, $this->month, 28, $this->year )+432000);
}
// Вычисляет следующий год от текущего календарного
public function getNextYear() {
return date('Y', mktime(0, 0, 0, $this->month, 28, $this->year )+432000);
}
// Вычисляет предыдущий месяц от текущего календарного
public function getPrevMonth() {
return date('m', mktime(0, 0, 0, $this->month, 1, $this->year )-432000);
}
// Вычисляет предыдущий год от текущего календарного
public function getPrevYear() {
return date('Y', mktime(0, 0, 0, $this->month, 1, $this->year )-432000);
}
// Формируем массив недель
private function makeWeeks($year, $month) {
$this->setYearMonth($year, $month);
$this->setDayOfMonth($this->year, $this->month);
$this->setFirstWeek();
// 1. Первая неделя
$this->num = 0;
for($i = 0; $i < 7; $i++) {
// Вычисляем номер дня недели для числа
$dayofweek = $this->getDayOfWeek();
// Приводим к числа к формату 1 - понедельник, ..., 6 - суббота
$dayofweek = $dayofweek - 1;
if($dayofweek == -1) $dayofweek = 6;
if($dayofweek == $i) {
// Если дни недели совпадают,
// заполняем массив $this->week
// числами месяца
$this->week[$this->num][$i] = $this->day_count;
$this->day_count++;
} else {
$this->week[$this->num][$i] = "";
}
}
// 2. Последующие недели месяца
while(true) {
$this->num++;
for($i = 0; $i < 7; $i++) {
$this->week[$this->num][$i] = $this->day_count;
$this->day_count++;
// Если достигли конца месяца - выходим
// из цикла
if($this->day_count > $this->dayofmonth) break;
}
// Если достигли конца месяца - выходим
// из цикла
if($this->day_count > $this->dayofmonth) break;
}
}
// Заголовок календаря
public function getCalendarHeader() {
$this->retunrhtml =
"<table cellpadding=2 cellspacing=0 border=1 style=\"margin-left: auto; margin-right: 0px;\">".
"<tbody>".
"<tr><th colspan=\"7\">".$this->month."/".$this->year."</th></tr>".
"<tr>".
"<th style=\"text-align: center;\">Пн</th>".
"<th style=\"text-align: center;\">Вт</th>".
"<th style=\"text-align: center;\">Ср</th>".
"<th style=\"text-align: center;\">Чт</th>".
"<th style=\"text-align: center;\">Пт</th>".
"<th style=\"text-align: center;\"><span style=\"color: rgb(255, 0, 0);\">Сб</span></th>".
"<th style=\"text-align: center;\"><span style=\"color: rgb(255, 0, 0);\">Вс</span></th>".
"</tr>";
}
// Окончание календаря
public function getCalendarFooter() {
$this->retunrhtml.="</tbody></table>";
}
// Начало новой недели
public function getBeginTR() {
$this->retunrhtml.="<tr>";
}
// Конец недели
public function getEndTR() {
$this->retunrhtml.="</tr>";
}
// Обрадатываемый день
protected function getDay() {
return $this->day;
}
// Обрадатываемый месяц
protected function getMonth() {
return $this->month;
}
// Обрадатываемый год
protected function getYear() {
return $this->year;
}
// Обрабатываемый день - выходной?
protected function isWeekend() {
return $this->weekend;
}
// Обрабатываемый день - текущий?
protected function isCurrent() {
return $this->currentdate;
}
// Значение ячейки
public function getTDHref() {
return $this->getDay();
}
// Ячейка буднего дня
public function getTD() {
$td="td"; if ($this->isCurrent()) $td="th";
$this->retunrhtml.="<$td style=\"text-align: right;\">".$this->getTDHref()."</$td>";
}
// Ячейка выходного дня
public function getTDWeekend() {
$td="td"; if ($this->isCurrent()) $td="th";
$this->retunrhtml.="<$td style=\"text-align: right;\"><font color=red>".$this->getTDHref()."</font></$td>";
}
// Получить код таблицы календаря
protected function makeCodeMonth($year, $month) {
$this->makeWeeks($year, $month);
$this->getCalendarHeader();
for($i = 0; $i < count($this->week); $i++) {
$this->getBeginTR();
for($j = 0; $j < 7; $j++) {
if(!empty($this->week[$i][$j])) {
// Обновляем информацию по обрабатывемому дню
$this->day = $this->week[$i][$j];
// Если имеем дело с текущим днем
$this->currentdate = 0;
if ( $this->year==date('Y') && $this->month==date('m') && $this->day==date('j')) $this->currentdate = 1;
// Если имеем дело с субботой и воскресенья подсвечиваем их
if($j == 5 || $j == 6) {
$this->weekend = 1;
$this->getTDWeekend();
} else {
$this->weekend = 0;
$this->getTD();
}
} else $this->retunrhtml.="<td> </td>";
}
$this->getEndTR();
}
$this->getCalendarFooter();
}
// Получить html-код обрадатываемого месяца
public function getCodeMonth() {
$this->makeCodeMonth($this->year, $this->month);
return $this->retunrhtml;
}
// Вывести html-код обрадатываемого месяца
public function showCodeMonth() {
echo $this->getCodeMonth();
}
}
class TechCalendarForm extends CalendarForm {
public function getTDHref() {
if ($this->isWeekend()) $font = "<font color=\"#FF3F4F\">"; else $font = "<font color=\"#4A5B6C\">";
return "<a href=\"".$_SERVER["PHP_SELF"]."?action=showdate&date=".parent::getYear()."-".parent::getMonth()."-".parent::getDay()."\">".$font.parent::getDay()."</font></a>";
}
}
?>