說到對時期和時間的處理,就一定要介紹一下日歷程序的編寫。但一提起編寫日歷,大多數人都會認為日歷的作用只是為了在頁上顯示當前的日期,其實日歷在我們的開發中有更重要的作用。例如我們開發一個“記事本”就需要通過日歷設定日期,還有一些系統中需要按日期去排任務,也需要日歷,等等。本例涉及的日期和時間函數並不是很多,都是前面介紹的內容,主要是通過一個日歷類的編寫,鞏固一下前面介紹過的面向對象的語法知識,以及時間函數應用,最主要的是可以提升初學者的思維邏輯和程序設計能力。將日歷類Calendar聲明在文件calendar.class.php中,代碼如下所示:

<?php
// file:calendar.class.php 日歷類原文件
error_reporting(0);
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");
//通過具體的年份和月份,利用date()函數的w參數獲取當月第一天對應的是周幾
$this->start_weekday = date("w",mktime(0,0,0,$this->month,1,$this->year));
//通過具體的年份和月份,利用date()函數的t參數獲取當月的天數
$this->days = date("t",mktime(0,0,0,$this->month,1,$this->year));
}
//魔術方法用於打印整個日歷
function __toString(){
$out .='<table align="center">';
$out .=$this->chageDate(); //調用內部私有方法用於用戶自己設置日期
$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>'; //第一行以表格<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) //每輸出7個日期,就換一行
$out .= '</tr><tr>'; //輸出行結束和下一行開始
}
//遍歷完日期後,將後面用空格補齊
while ($j%7 !== 0){
$out .= '<td> </td>';
$j++;
}
$out .= '</tr>';
return $out; //返回當月日期列表
}
//內部調用的私有方法,用於處理當前年份的上一年需要的數據
private function prevYear($year,$month){
$year = $year-1; //上一年是當前年減1
if($year < 1970) //年份設置最小值是1970年
$year = 1970;
return "year={$year}&month={$month}"; //返回最終的年份和月份設置參數
}
//內部調用的私有方法,用於處理當前月份的上一月份需要的數據
private function prevMonth($year,$month){
if ($month == 1){
$year = $year-1; //上一年是當前年減1
if($year < 1970) //年份設置最小值是1970年
$year =1970;
$month = 12; //如果是1月,上一月就是上一年的最後一月
}else {
$month--; //上一月份是當前月減1
}
return "year={$year}&month={$month}"; //返回最終的年份和月份設置參數
}
//內部調用的私有方法,用於處理當前年份的下一年份的數據
private function nextYear($year,$month){
$year = $year+1; //下一年是當前年加1
if($year > 2038) //年份設置最大值是2038年
$year =2038;
return "year={$year}&month={$month}"; //返回最終的年份和月份設置參數
}
//內部調用的私有方法,用於處理當前月份的下一月份需要的數據
private function nextMonth($year,$month){
if ($month == 12){
$year++;
if($year > 2038) //年份設置最大值是2038年
$year =2038;
$month = 1; //如果是1月,上一月就是上一年的最後一月
}else {
$month++; //上一月份是當前月減1
}
return "year={$year}&month={$month}"; //返回最終的年份和月份設置參數
}
//內部調用的私有方法,用於用戶操作去調整年份和月份的設置
private function chageDate($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->nextYear($this->year,$this->month).'">'.'>>'.'</a></td>';
$out .= '<td><a href="'.$url.'?'.$this->nextMonth($this->year,$this->month).'">'.'>>'.'</a></td>';
$out .= '</tr>';
return $out; //返回日期表單
}
}
?>
本例將一個日歷程序按功能拆分(周列表部分、日期列表部分、設置日期部分,以及上一年、下一年、上一月和下一月的設置部分)並封裝在一個日歷類中。有了日歷類,我們還需要再編寫一個主程序去加載並輸出日歷,在主程序中還需要先設置一下日歷輸出的樣式,代碼如下所示:
<html>
<head>
<title>恩聰PHP日歷示例</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>
運行結果如圖所示,默認顯示當前系統日期。可以通過單擊“>>”按鈕設置下一年份,但設置的最大年份為2038年。也可以通過單擊“<<”按鈕設置上一年份,但設置的最小年份為1970年。還可以通過單擊“<”各“>”按鈕設置上一個和下一個月份,如果當月為12月,則設置的下一個月份就為次年的1月,如果當月為1月,則設置上一個月份就為上一年的12月。如果需要快速定位到指定的年份和月份,還可通過下拉列表進行設置。