<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>万年历的实现</title>
</head>
<body>
<?php
// 1.获取日期信息年和月(默认为当前的年和当前的月)
$year=$_GET["y"]?$_GET["y"]:date("Y");
$mon=$_GET["m"]?$_GET["m"]:date("m");
//2.计算出当前月有多少天和本月1号是星期几
$day=date("t",mktime(0,0,0,$mon,1,$year));//获取当月对应的天数
$w=date("w",mktime(0,0,0,$mon,1,$year));//获取当月1号是星期几
// 3.输出日期的头部信息(标题和表头)
echo"<center>";
echo"<h1>{$year}年{$mon}月</h1>";
echo"<table width='600' border='1'>";
echo"<tr>";
echo"<th style='color:red'>星期日</th>";
echo"<th>星期一</th>";
echo"<th>星期二</th>";
echo"<th>星期三</th>";
echo"<th>星期四</th>";
echo"<th>星期五</th>";
echo"<th style='color:green'>星期六</th>";
echo"</tr>";
//4.循环遍历输出日期信息
$dd=1;//定义一个循环的天数
while($dd<=$day){
echo"<tr>";
//输出一周的信息
for($i=0;$i<7;$i++){
//当还没有到达该输出日期的时候,或已经日期溢出时,输出的都是空单元格
if(($w>$i&&$dd==1)||$dd>$day){
echo"<td> </td>";
}else{
echo"<td>{$dd}</td>";
$dd++;
}
}
echo"</tr>";
}
echo"</table>";
//5.输出上一月和下一月的超连接
//处理上一月和下一月的信息
$prey=$nexty=$year;
$prem=$nextm=$mon;
if($prem<=1){
$prem=12;
$prey--;
}else{
$prem--;
}
if($nextm>=12){
$nextm=1;
$nexty++;
}else{
$nextm++;
}
// $prem=$mon-1;
// $nextm=$mon+1;
echo"<h3><a href='date.php?y={$prey}&m={$prem}' >上一月</a> ";
echo"<a href='date.php?y={$nexty}&m={$nextm}' >下一月</a></h3>";
echo"</center>";
?>
</body>
</html>