说到对时期和时间的处理,就一定要介绍一下日历程序的编写。但一提起编写日历,大多数人都会认为日历的作用只是为了在页上显示当前的日期,其实日历在我们的开发中有更重要的作用。例如我们开发一个“记事本”就需要通过日历设定日期,还有一些系统中需要按日期去排任务,也需要日历,等等。本例涉及的日期和时间函数并不是很多,都是前面介绍的内容,主要是通过一个日历类的编写,巩固一下前面介绍过的面向对象的语法知识,以及时间函数应用,最主要的是可以提升初学者的思维逻辑和程序设计能力。将日历类Calendar声明在文件calendar.class.php中,代码如下所示:
<?php
// file:calendar.class.php 日历类原文件
error_reporting(0);
classCalendar{
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.="<tablealign="center">";
$out.=$this->chageDate(); //调用内部私有方法用于用户自己设置日期
$out.=$this->weeksList(); //调用内部私有方法打印周列表
$out.=$this->daysList(); //调用内部私有方法打印日列表
$out.="</table>";
return$out; //返回整个日历输需要的全部字符串
}
//内部调用的私有方法,用于输出周列表
privatefunctionweeksList(){
$week=array("日","一","二","三","四","五","六");
$out.="<tr>";
for($i=0;$i<count($week);$i++)
$out.="<thclass="fontb">".$week[$i]."</th>"; //第一行以表格<th>输出周列表
$out.="</tr>";
return$out; //返回周列表字符串
}
//内部调用的私有方法,用于输出周列表
privatefunctiondaysList(){
$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.="<tdclass="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; //返回当月日期列表
}
//内部调用的私有方法,用于处理当前年份的上一年需要的数据
privatefunctionprevYear($year,$month){
$year=$year-1; //上一年是当前年减1
if($year<1970) //年份设置最小值是1970年
$year=1970;
return"year={$year}&month={$month}"; //返回最终的年份和月份设置参数
}
//内部调用的私有方法,用于处理当前月份的上一月份需要的数据
privatefunctionprevMonth($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}"; //返回最终的年份和月份设置参数
}
//内部调用的私有方法,用于处理当前年份的下一年份的数据
privatefunctionnextYear($year,$month){
$year=$year+1; //下一年是当前年加1
if($year>2038) //年份设置最大值是2038年
$year=2038;
return"year={$year}&month={$month}"; //返回最终的年份和月份设置参数
}
//内部调用的私有方法,用于处理当前月份的下一月份需要的数据
privatefunctionnextMonth($year,$month){
if($month==12){
$year++;
if($year>2038) //年份设置最大值是2038年
$year=2038;
$month=1; //如果是1月,上一月就是上一年的最后一月
}else{
$month++; //上一月份是当前月减1
}
return"year={$year}&month={$month}"; //返回最终的年份和月份设置参数
}
//内部调用的私有方法,用于用户操作去调整年份和月份的设置
privatefunctionchageDate($url="index.php"){
$out.="<tr>";
$out.="<td><ahref="".$url."?".$this->prevYear($this->year,$this->month)."">"."<<"."</a></td>";
$out.="<td><ahref="".$url."?".$this->prevMonth($this->year,$this->month)."">"."<<"."</a></td>";
$out.="<tdcolspan="3">";
$out.="<form>";
$out.="<selectname="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.="<selectname="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><ahref="".$url."?".$this->nextYear($this->year,$this->month)."">".">>"."</a></td>";
$out.="<td><ahref="".$url."?".$this->nextMonth($this->year,$this->month)."">".">>"."</a></td>";
$out.="</tr>";
return$out; //返回日期表单
}
}
?>
本例将一个日历程序按功能拆分(周列表部分、日期列表部分、设置日期部分,以及上一年、下一年、上一月和下一月的设置部分)并封装在一个日历类中。有了日历类,我们还需要再编写一个主程序去加载并输出日历,在主程序中还需要先设置一下日历输出的样式,代码如下所示:
<html>
<head>
<title>恩聪PHP日历示例</title>
<style>
table{border:1pxsolid#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";
echonewcalendar;
?>
</body>
</html>
运行结果如图所示,默认显示当前系统日期。可以通过单击“>>”按钮设置下一年份,但设置的最大年份为2038年。也可以通过单击“<<”按钮设置上一年份,但设置的最小年份为1970年。还可以通过单击“<”各“>”按钮设置上一个和下一个月份,如果当月为12月,则设置的下一个月份就为次年的1月,如果当月为1月,则设置上一个月份就为上一年的12月。如果需要快速定位到指定的年份和月份,还可通过下拉列表进行设置。