龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

简单实现日历

时间:2014-07-22 14:52来源: 作者: 点击:
分享到:
一个简单实现的日历,我不知道这段代码实现的方法有没有问题,没有参考前辈,等你理解我的烂代码之后,再欣赏一下别人的优秀代码,会更有帮助br /
一个简单实现的日历,我不知道这段代码实现的方法有没有问题,没有参考前辈,等你理解我的烂代码之后,再欣赏一下别人的优秀代码,会更有帮助

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
<style>
#calendardiv,#calendar{width:252px;}
#cal_title{height:33px;line-height:33px;text-align:center;overflow:hidden;}
#cal_title strong{font-weight:bold;font-size:14px; }
#cal_title a{font-weight:bold;font-size:14px;text-decoration:none;}
#calendar{border-collapse:collapse;}
#calendar td{
	text-align:center;
	width:35px;
	height:20px;
	line-height:20px;
	background-color:#efefef;
	border-bottom:1px solid #fff;
	border-right:1px solid #fff;
	}
#calendar .even td{background-color:#e6e6e6;}
#calendar td .current{display:block;background-color:#f60;color:#fff;}
#calendar .current{background-color:#f60!important;color:#fff;}
#week td{color:#fff;background-color:#373737;}
</style>
</head>
<body>
<?php
	$date = isset($_GET['d']) ? intval($_GET['d']) : '';
	if($date)
	{
		$y = substr($date,0,4);
		$m = substr($date,4,2);
		$cur = mktime(0,0,0,$m,1,$y);
	}
	else
	{
		$cur = mktime();
	}

	list($year,$month,$day) = explode('-',date('Y-m-d',$cur));//年月日
	$p = date('Ym',strtotime('last months',$cur));//前一月
	$n = date('Ym',strtotime('next months',$cur));//后一月
	$t = date('t',$cur); //当月多少天
	$s = date('w',mktime(0,0,0,$month,1,$year)); //前补空白
	$e = 6-(date('w',mktime(0,0,0,$month,$t,$year)));//后补空白
?>
<div id="calendardiv">
<div id="cal_title"><a href="?d=<?=$p?>" title="上一月">&laquo;</a>&nbsp;&nbsp;<strong><?=$year?>年<?=$month?>月</strong>&nbsp;&nbsp;<a href="?d=<?=$n?>" title="下一月">&raquo;</a></div>
<table id="calendar">
<tr id="week"><td>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr>
<?php
	echo '<tr class="even">';
	for($i=0;$i<$s;$i++)
	{
		echo '<td>&nbsp;</td>';
	}
	for($d=1;$d<=$t;$d++)
	{
		$current=$d==$day?'class="current"':'';//当前样式
		$r = ($d+$s)%7;//换行

		echo "<td $current >$d</td>";
		if($r==0)
		{
			echo '</tr>';
			echo '<tr class="even">';
		}
	}
	for($i=0;$i<$e;$i++)
		{
			echo '<td>&nbsp;</td>';
		}
?>
</tr></table></div>
精彩图集

赞助商链接