基於PHP的日曆程式

smileNicky發表於2016-03-15


<head>
<style>
th{font-family:隸書;color:#0099ff;}
table{background:#99ffcc;}
#tr1{background:#00ccff;}
.td1{color:#009999;}
a{color:#ff9900;}
</style>
</head>
<body>
<?php
//php日曆
//1.date()函式獲取當前的年月
$year=$_GET["y"]?$_GET["y"]:date("Y");
$mon=$_GET["m"]?$_GET["m"]:date("m");
//2.mktime()函式的使用,獲取當前月的天數及當月1號的星期
$day=date("t",mktime(0,0,0,$mon,1,$year));
$w=date("w",mktime(0,0,0,$mon,1,$year));
//3.輸出日曆的頭部資訊
echo"<center>";
echo"<table border='0'>";
echo"<h3><font color='#00ff33'font-face='隸書'>{$year}年{$mon}月</font></h3>";
echo "<tr id='tr1'onmouseOver='overTr(this)'onmouseOut='outTr(this)'>";
echo "<th style='color:#ff0000;'onmouseOver='overTr(this)'onmouseOut='outTr(this)'>日</th>";
echo "<th class='td1'>一</th>";
echo "<th class='td1'>二</th>";
echo "<th class='td1'>三</th>";
echo "<th class='td1'>四</th>";
echo "<th class='td1'>五</th>";
echo "<th style='color:#ff0000;'>六</th>";
echo "</tr>";
//4.遍歷輸出日曆
$d=1;
while($d<=$day){
	echo"<tr onmouseOver='overTr(this)'onmouseOut='outTr(this)'>";
	for($i=1;$i<=7;$i++){//迴圈輸出7天資訊
		if($d<=$day&&($w<=$i||$d!=1)){
			echo "<th>{$d}</th>";
			$d++;
		}else{
			echo"<th> </th>";
		}
	}
}
//5.處理上下月,上下年的資訊
$prey=$nexty=$year;
$prem=$nextm=$mon;
if($prem<=1){
$prem=12;
$prey--;
}else{
$prem--;
}
if($nextm>=12){
$nextm=1;
$nexty++;
}else{
$nextm++;
}

$prey=$year-1;//上一年
$nexty=$year+1;//上一月
//超連結
echo "<tr  onmouseOver='overTr(this)'onmouseOut='outTr(this)'><td colspan='7'align='center'>";

echo"<a href='sy1.php?y={$prey}'><<</a> ";
echo "<font face='隸書'color='#663399'>{$year}年</font> ";
echo "<a href='sy1.php?y={$nexty}'>>></a>  ";

echo"  ";

echo"<a href='sy1.php?m={$prem}'><</a> ";
echo "<font face='隸書'color='#663399'>{$mon}月</font> ";
echo "<a href='sy1.php?m={$nextm}'>></a>";

echo "</td></tr>";
echo"</table>";
echo"</center>";
?>

<script type='text/JavaScript'>
var oriCol=null;
function  overTr(obj){
oriCol=obj.bgColor;
obj.bgColor='#00ff99';
}
function outTr(obj){
obj.bgColor=oriCol;	
}

</script>
</body>
</html>

相關文章