php实现简单的万年历

用php实现简单万年历效果,深入了解php的日期操作。如下图所示:

php实现简单的万年历_第1张图片

<!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>
<style type="text/css">
*{padding:0; margin:0;}
body{font: 14px/1.5 Tahoma,'Simsun';}
a{color:#06C;font-size:12px; text-decoration:none;}
#talbe_row{width:600px; border-collapse:collapse;}
#table_row th,#table_row td{height:32px; line-height:32px; text-align:center;}
</style>
</head>

<body>
<center>
<?php

$year=$_GET["year"]?$_GET["year"]:date("Y");
$month=$_GET["month"]?$_GET["month"]:date("m");
$time=mktime(0,0,0,$month,1,$year);
$totalday=date("t",$time);
$weekday=date('w',$time);
echo "<h2>万年历实例</h2>";
echo "<h3>{$year}年{$month}月</h3>";
echo "<table width='600' border=1 cellpadding='0' cellspacing='0' id='table_row'>";
echo "<tr bgcolor='#ddd'>";
echo "<th>星期天</th><th>星期一</th><th>星期二</th><th>星期三</th><th>星期四</th><th>星期五</th><th>星期六</th>";
echo "</tr>";
$i=1;
while($i<=$totalday){
	echo "<tr>";
	
	for($j=0;$j<7;$j++){
		if(($j<$weekday && $i==1) || ($i>$totalday)){
		echo "<td> </td>";
		}else{
		echo "<td>{$i}</td>";
		$i++;
		}}
	
	echo "</tr>";
	
}
echo "</table>";
echo "<p></p>";
$prevy=$nexty=$year;

$prem=$nextm=$month;
$prem--;
if($prem<1){
	$prem=12;
	$prevy--;
	}
$nextm++;
if($nextm>12){
	$nextm=1;
	$nexty++;
	}



echo "<p></p>";
echo "<a href='?year={$prevy}&month={$prem}'>上一月</a>   <a href='?year={$nexty}&month={$nextm}'>下一月</a>";
?>
</center>
</body>
</html>

  

你可能感兴趣的:(PHP)