calendar.class.php
<?php
/*
*创建一个日历类
*
*
*/
//修改默认时区
date_default_timezone_set("PRC");
classCalendar{
private$year;
private$month;
private$day;//当月总天数
private$first_week;//每月的第一天是星期几
//构造函数
function__construct(){
$this->year=isset($_GET['year'])?$_GET['year']:date("Y");
$this->month=isset($_GET["month"])?$_GET["month"]:date("m");
$this->first_week=date("w",mktime(0,0,0,$this->month,1,$this->year));
$this->day=date("t",mktime(0,0,0,$this->month,1,$this->year));
}
functionshowCalendar(){
//echo$this->year."年".$this->month."月".$this->first_week."天".$this->day;
echo"<tablealign='center'>";//用表格输出
$this->chageDate("index.php");//用于用户调整年月份
$this->weekList();//显示星期
$this->dayList();//显示天数
echo"</table>";
}
//1、显示星期
privatefunctionweekList(){
$week=array("日","一","二","三","四","五","六");
echo"<tr>";
for($i=0;$i<count($week);$i++){
echo"<th>".$week[$i]."</th>";
}
echo"</tr>";
}
//2.显示天数
privatefunctiondayList(){
$color="#2ca50c";
echo"<tr>";
for($i=0;$i<$this->first_week;$i++){//输出空格,弥补当前月空缺部分
echo"<tdbgcolor='#2ca50c'></td>";
}
for($k=1;$i<=$this->day;$k++){
$i++;
if($k==date("d"))echo"<tdid='nowd'>".$k."</td>";//是今天,加效果
elseecho"<tdbgcolor=$color>".$k."</td>";
if($i%7==0){
echo"</tr><tr>";//每7天一次换行
if($i%2==0)$color="#2ca50c";
else$color="#9ddb27";//实现各行换色的效果
}
}
while($i%7!=0){//将剩余的空格补完
echo"<tdbgcolor='#2ca50c'></td>";
$i++;
}
echo"</tr>";
}
//3、用于用户调整天数
privatefunctionchageDate($url="index.php"){
echo"<tr>";
echo"<caption><h1>".$this->year."年".$this->month."月</h1></caption>";
echo"</tr>";
echo"<tr>";
echo"<td>"."<ahref='?".$this->prevYear($this->year,$this->month)."'>"."<"."</a>";
echo"<td>"."<ahref='?".$this->prevMonth($this->year,$this->month)."'>"."<<"."</a>";
echo"<tdcolspan='3'>";
echo'<selectοnchange="window.location=\''.$url.'?year=\'+this.options[selectedIndex].value+\'&month='.$this->month.'\'">';
for($year=2038;$year>=1970;$year--){
$selected=($year==$this->year)?"selected":"";
echo'<option'.$selected.'value="'.$year.'">'.$year.'</option>';
//echo'<option'.$selected.'value="'.$year.'">'.$year.'</option>';
}
echo"</select>";
echo'<selectname="month"οnchange="window.location=\''.$url.'?year='.$this->year.'&month=\'+this.options[selectedIndex].value">';
for($month=1;$month<=12;$month++){
$selected1=($month==$this->month)?"selected":"";
echo'<option'.$selected1.'value="'.$month.'">'.$month.'</option>';
}
echo'</select>';
echo"</td>";
echo"<td>"."<ahref='?".$this->nextMonth($this->year,$this->month)."'>".">>"."</a>";
echo"<td>"."<ahref='?".$this->nextYear($this->year,$this->month)."'>".">"."</a>";
echo"</tr>";
}
privatefunctionprevYear($year,$month){//获取上一年的数据
$year--;
if($year<1970)$year=1970;
return"year={$year}&month={$month}";
}
privatefunctionprevMonth($year,$month){
if($month==1){
$year--;
if($year<1970)$year=1970;
$month=12;
}else$month--;
return"year={$year}&month={$month}";
}
privatefunctionnextYear($year,$month){//获取上一年的数据
$year++;
if($year>2038)$year=2038;
return"year={$year}&month={$month}";
}
privatefunctionnextMonth($year,$month){
if($month==12){
$year++;
if($year>2038)$year=2038;
$month=1;
}else$month++;
return"year={$year}&month={$month}";
}
}
主页index.php
<!doctypehtml>
<html>
<head>
<metacharset="utf-8">
<title>日历显示</title>
<style>
table{
border:1pxsolid#050;
margin:100pxauto;
}
th{
width:30px;
background-color:#0CC;
color:#fff;
height:30px;
font-size:20px;
}
#nowd{
color:yellow;
background:#F00;
}
td{
width:30px;
text-align:center;
height:25px;
color:#fff;
}
a{
display:block;
width:35px;
height:35px;
background:#0F9;
text-decoration:none;
text-align:center;
line-height:35px;
}
a:hover{
background:#CF0;
color:#fff;
font-size:20px;
}
</style>
</head>
<body>
<?php
include"calendar.class.php";
$ca=newCalendar();
$ca->showCalendar();
?>
</body>
</html>
|