PHP Calendar Class Tutorial with Full Implementation
This tutorial explains how to build a reusable PHP Calendar class that handles year, month, navigation and renders an HTML table, and shows a simple front‑end script that loads the class and displays the calendar, complete with CSS styling and example output.
When handling dates and times in web applications, a reusable calendar component is essential, not only for displaying the current date but also for tasks such as scheduling and note‑taking.
The article defines a Calendar class in calendar.class.php, which stores the year, month, start weekday and total days, provides methods to generate the week header, day cells, navigation links for previous/next month and year, and renders the whole calendar as an HTML table.
Key methods include __construct() for initializing properties from URL parameters, __toString() to output the table, weeksList(), daysList(), and helper functions prevYear(), prevMonth(), nextYear(), nextMonth(), and changeDate() for building the navigation form.
A simple front‑end script loads the class, echoes a new Calendar instance, and applies basic CSS to style the table, producing a functional calendar view as shown in the screenshot.
<?php
/* calendar.class.php日历类
声明一个日历类,名称为Calendar,用来显示可以设置日期的日历
*/
class Calendar{
private $year;//当前的年
private $month;//当前的月
private $start_weekday;//当月的第一天对应的是周几,作为当月遍历日期的开始
private $days;//当前月的总天数
/** 构造方法,初始化一些属性 */
function __construct(){
$this->year = isset($_GET["year"]) ? $_GET["year"] :date("Y") ;
$this->month = isset($_GET["month"]) ? $_GET["month"] :date("m") ;
$this->start_weekday = date("w",mktime(0, 0, 0, $this->month, 1, $this->year));
$this->days = date("t",mktime(0, 0, 0, $this->month, 1, $this->year));
}
/** 打印整个日历 */
function __toString(){
$out .= '<table align="center">';
$out .= $this->changeDate();
$out .= $this->weeksList();
$out .= $this->daysList();
$out .= '</table>';
return $out;
}
/** 输出周列表 */
private function weeksList(){
$week = array ('日','一','二','三','四','五','六');
$out .= '<tr>';
for($i = 0; $i < count($week); $i++){
$out .= '<th class="fontb">' . $week [$i]. '</th>';
}
$out .= '</tr>';
return $out;
}
/** 输出日列表 */
private function daysList(){
$out .= '<tr>';
for($j = 0; $j < $this->start_weekday; $j++){
$out .= '<td> </td>';
}
for($k = 1; $k <= $this->days; $k++){
$j++;
if($k == date('d')){
$out .= '<td class="fontb">'.$k.'</td>';
} else {
$out .= '<td>'.$k.'</td>';
}
if($j%7 == 0){
$out .= '</tr><tr>';
}
}
while ($j%7 != 0) {
$out .= '<td> </td>';
$j++;
}
$out .= '</tr>';
return $out;
}
private function prevYear($year, $month){
$year = $year-1;
if ($year < 1970){ $year = 1970; }
return "year={$year}&month={$month}";
}
private function prevMonth($year, $month){
if($month== 1){
$year = $year -1;
if($year < 1970){ $year = 1970; }
$month = 12;
} else {
$month--;
}
return "year={$year}&month={$month}";
}
private function nextYear($year, $month){
$year = $year+1;
if ($year> 2038){ $year=2038; }
return "year={$year}&month={$month}";
}
private function nextMonth($year, $month){
if($month == 12){
$year++;
if($year> 2038){ $year = 2038; }
$month = 1;
} else {
$month++;
}
return "year={$year}&month={$month}";
}
private function changeDate($url='index.php'){
$out .= '<tr>';
$out .= '<td><a href="'.$url.'?'.$this->prevYear($this->year,$this->month).'"><<</a></td>';
$out .= '<td><a href="'.$url.'?'.$this->prevMonth($this->year,$this->month).'"<</a> </td>';
$out .= '<td colspan="3">';
$out .= '<form>';
$out .= '<select name="year" onchange="window.location=\''.$url.'?year=\'+this.options[selectedIndex].value+\'&month='.$this->month.'\'">';
for($sy=1970; $sy <= 2038; $sy++){
$selected = ($sy==$this->year) ? "selected" : "";
$out .= '<option '.$selected.' value="'.$sy.'">'.$sy.'</option>';
}
$out .= '</select>';
$out .= '<select name="month" onchange="window.location=\''.$url.'?year='.$this->year.'&month=\'+this.options[selectedIndex].value">';
for ($sm=1; $sm <=12; $sm++){
$selected1 = ($sm==$this->month) ? "selected" : "";
$out .= '<option '.$selected1.' value="'.$sm.'">'.$sm.'</option>';
}
$out .= '</select>';
$out .= '</form>';
$out .= '</td>';
$out .= '<td> <a href="'.$url.'?'.$this->nextMonth($this->year,$this->month).'">></a></td>';
$out .= '<td> <a href="'.$url.'?'.$this->nextYear($this->year,$this->month).'">>></a></td>';
$out .= '</tr>';
return $out;
}
}
?> <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>日历示例</title>
<style>
table{ border: 1px solid #050; }
.fontb{ color:white;background:blue; }
th { width: 30px; }
td,th { height: 30px;text-align:center; }
form { margin:0px;padding:0px; }
</style>
</head>
<body>
<?php
require 'calendar.class.php';
echo new Calendar;
?>
</body>
</html>Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
