一個(gè)比較不錯(cuò)的PHP日歷類分享
說到對(duì)時(shí)期和時(shí)間的處理,就一定要介紹一下日歷程序的編寫。但一提起編寫日歷,大多數(shù)人都會(huì)認(rèn)為日歷的作用只是為了在頁上顯示當(dāng)前的日期,其實(shí)日歷在我們的開發(fā)中有更重要的作用。例如我們開發(fā)一個(gè)“記事本”就需要通過日歷設(shè)定日期,還有一些系統(tǒng)中需要按日期去排任務(wù),也需要日歷,等等。本例涉及的日期和時(shí)間函數(shù)并不是很多,都是前面介紹的內(nèi)容,主要是通過一個(gè)日歷類的編寫,鞏固一下前面介紹過的面向?qū)ο蟮恼Z法知識(shí),以及時(shí)間函數(shù)應(yīng)用,最主要的是可以提升初學(xué)者的思維邏輯和程序設(shè)計(jì)能力。將日歷類Calendar聲明在文件calendar.class.php中,代碼如下所示:

<?php
// file:calendar.class.php 日歷類原文件
error_reporting(0);
class Calendar{
private $year;
private $month;
private $start_weekday; //當(dāng)月的第一天對(duì)應(yīng)的是周幾,作為當(dāng)月開始遍歷日期的開始
private $days; //當(dāng)前月總天數(shù)
//構(gòu)造方法,用來初使化一些日期屬性
function __construct(){
//如果用戶沒有設(shè)置所份數(shù),則使用當(dāng)前系統(tǒng)時(shí)間的年份
$this->year = isset($_GET["year"]) ? $_GET["year"] : date("Y");
//如果用戶沒有設(shè)置月份數(shù),則使用當(dāng)前系統(tǒng)時(shí)間的月份
$this->month = isset($_GET["month"]) ? $_GET["month"] : date("m");
//通過具體的年份和月份,利用date()函數(shù)的w參數(shù)獲取當(dāng)月第一天對(duì)應(yīng)的是周幾
$this->start_weekday = date("w",mktime(0,0,0,$this->month,1,$this->year));
//通過具體的年份和月份,利用date()函數(shù)的t參數(shù)獲取當(dāng)月的天數(shù)
$this->days = date("t",mktime(0,0,0,$this->month,1,$this->year));
}
//魔術(shù)方法用于打印整個(gè)日歷
function __toString(){
$out .='<table align="center">';
$out .=$this->chageDate(); //調(diào)用內(nèi)部私有方法用于用戶自己設(shè)置日期
$out .=$this->weeksList(); //調(diào)用內(nèi)部私有方法打印周列表
$out .=$this->daysList(); //調(diào)用內(nèi)部私有方法打印日列表
$out .='</table>';
return $out; //返回整個(gè)日歷輸需要的全部字符串
}
//內(nèi)部調(diào)用的私有方法,用于輸出周列表
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; //返回周列表字符串
}
//內(nèi)部調(diào)用的私有方法,用于輸出周列表
private function daysList(){
$out .= '<tr>';
//輸出空格(當(dāng)前一月第一天前面要空出來)
for ($j = 0; $j<$this->start_weekday; $j++)
$out .= '<td> </td>';
//將當(dāng)月的所有日期循環(huán)遍歷出來,如果是當(dāng)前日期,為其設(shè)置深色背景
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個(gè)日期,就換一行
$out .= '</tr><tr>'; //輸出行結(jié)束和下一行開始
}
//遍歷完日期后,將后面用空格補(bǔ)齊
while ($j%7 !== 0){
$out .= '<td> </td>';
$j++;
}
$out .= '</tr>';
return $out; //返回當(dāng)月日期列表
}
//內(nèi)部調(diào)用的私有方法,用于處理當(dāng)前年份的上一年需要的數(shù)據(jù)
private function prevYear($year,$month){
$year = $year-1; //上一年是當(dāng)前年減1
if($year < 1970) //年份設(shè)置最小值是1970年
$year = 1970;
return "year={$year}&month={$month}"; //返回最終的年份和月份設(shè)置參數(shù)
}
//內(nèi)部調(diào)用的私有方法,用于處理當(dāng)前月份的上一月份需要的數(shù)據(jù)
private function prevMonth($year,$month){
if ($month == 1){
$year = $year-1; //上一年是當(dāng)前年減1
if($year < 1970) //年份設(shè)置最小值是1970年
$year =1970;
$month = 12; //如果是1月,上一月就是上一年的最后一月
}else {
$month--; //上一月份是當(dāng)前月減1
}
return "year={$year}&month={$month}"; //返回最終的年份和月份設(shè)置參數(shù)
}
//內(nèi)部調(diào)用的私有方法,用于處理當(dāng)前年份的下一年份的數(shù)據(jù)
private function nextYear($year,$month){
$year = $year+1; //下一年是當(dāng)前年加1
if($year > 2038) //年份設(shè)置最大值是2038年
$year =2038;
return "year={$year}&month={$month}"; //返回最終的年份和月份設(shè)置參數(shù)
}
//內(nèi)部調(diào)用的私有方法,用于處理當(dāng)前月份的下一月份需要的數(shù)據(jù)
private function nextMonth($year,$month){
if ($month == 12){
$year++;
if($year > 2038) //年份設(shè)置最大值是2038年
$year =2038;
$month = 1; //如果是1月,上一月就是上一年的最后一月
}else {
$month++; //上一月份是當(dāng)前月減1
}
return "year={$year}&month={$month}"; //返回最終的年份和月份設(shè)置參數(shù)
}
//內(nèi)部調(diào)用的私有方法,用于用戶操作去調(diào)整年份和月份的設(shè)置
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; //返回日期表單
}
}
?>
本例將一個(gè)日歷程序按功能拆分(周列表部分、日期列表部分、設(shè)置日期部分,以及上一年、下一年、上一月和下一月的設(shè)置部分)并封裝在一個(gè)日歷類中。有了日歷類,我們還需要再編寫一個(gè)主程序去加載并輸出日歷,在主程序中還需要先設(shè)置一下日歷輸出的樣式,代碼如下所示:
<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>
運(yùn)行結(jié)果如圖所示,默認(rèn)顯示當(dāng)前系統(tǒng)日期。可以通過單擊“>>”按鈕設(shè)置下一年份,但設(shè)置的最大年份為2038年。也可以通過單擊“<<”按鈕設(shè)置上一年份,但設(shè)置的最小年份為1970年。還可以通過單擊“<”各“>”按鈕設(shè)置上一個(gè)和下一個(gè)月份,如果當(dāng)月為12月,則設(shè)置的下一個(gè)月份就為次年的1月,如果當(dāng)月為1月,則設(shè)置上一個(gè)月份就為上一年的12月。如果需要快速定位到指定的年份和月份,還可通過下拉列表進(jìn)行設(shè)置。
相關(guān)文章
適用于抽獎(jiǎng)程序、隨機(jī)廣告的PHP概率算法實(shí)例
做網(wǎng)站類的有時(shí)會(huì)弄個(gè)活動(dòng)什么的,來讓用戶參加,既吸引用戶注冊(cè),又提高網(wǎng)站的用戶活躍度。同時(shí)參加的用戶會(huì)獲得一定的獎(jiǎng)品,有100%中獎(jiǎng)的,也有按一定概率中獎(jiǎng)的,大的比如中個(gè)ipad、iphone5,小的中個(gè)Q幣什么的2014-04-04
Yii2框架數(shù)據(jù)庫簡(jiǎn)單的增刪改查語法小結(jié)
這篇文章主要介紹了Yii2框架數(shù)據(jù)庫簡(jiǎn)單的增刪改查語法小結(jié),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08
TP5框架請(qǐng)求響應(yīng)參數(shù)實(shí)例分析
這篇文章主要介紹了TP5框架請(qǐng)求響應(yīng)參數(shù),結(jié)合實(shí)例形式分析了thinkPHP5請(qǐng)求參數(shù)及相應(yīng)參數(shù)的獲取方法,需要的朋友可以參考下2019-10-10
Laravel實(shí)現(xiàn)登錄跳轉(zhuǎn)功能
網(wǎng)站管理員登錄成功后跳轉(zhuǎn)到網(wǎng)站后臺(tái),vip用戶登錄成功后跳轉(zhuǎn)到vip頁面,跳轉(zhuǎn)功能怎么實(shí)現(xiàn)的呢,下面小編給大家?guī)砹薒aravel實(shí)現(xiàn)登錄跳轉(zhuǎn)功能,需要的朋友可以參考下2021-08-08
實(shí)用的PHP帶公鑰加密類分享(每次加密結(jié)果都不一樣哦)
這篇文章主要介紹了實(shí)用的PHP帶公鑰加密類分享,本類實(shí)現(xiàn)每次的加密結(jié)果都不一樣,但解密沒有問題,非常實(shí)用的一個(gè)加密類,需要的朋友可以參考下2014-08-08

