使用PHP制作一个万年历

主要运用date函数和mktime函数

完成结果如下:


代码如下:

<?php 
	header("Content-type:text/html;charset=utf-8");
	$year = isset($_GET['y'])?$_GET['y']:date("Y");
	$mon = isset($_GET['m'])?$_GET['m']:date("m");
	$day = date("t",mktime(0,0,0,$mon,1,$year));//获取对应月份总共有多少天

	$w = date("w",mktime(0,0,0,$mon,1,$year));//获取对应月份中1号是星期几
	$c = "<center>";
	$c .= "<h1>{$year}年{$mon}月</h1>";
	$c .= "<table border=1>";
	$c .= "<tr>";
	$c .= "<td>星期日</td>";
	$c .= "<td>星期一</td>";
	$c .= "<td>星期二</td>";
	$c .= "<td>星期三</td>";
	$c .= "<td>星期四</td>";
	$c .= "<td>星期五</td>";
	$c .= "<td>星期六</td>";
	$c .= "</tr>";
	$dd = 1;
	while ($dd <= $day) {
		$c .= "<tr>";
		for ($i=0; $i < 7; $i++) {
			if (($dd>$day) || ($dd==1 && $w>$i)) {
				$c .= "<td></td>";
			}else{
				$c .= "<td>{$dd}</td>";
				$dd++;
			}
		}
		$c .= "</tr>";
			
	}


	$c .= "</center></table>";
	echo $c;
$yearList = '<option value="">请选择</option>';
for ($i=1990; $i < 2050; $i++) {
	$select = '';
	if($year==$i){
		$select = 'selected="selected"';
	}
	$yearList .= '<option value="'.$i.'" '.$select.'>'.$i.'</option>';
}
$monList = '<option value="">请选择</option>';
for ($i=1; $i < 12; $i++) {
	$select = '';
	if($mon==$i){
		$select = 'selected="selected"';
	} 
	$monList .= '<option value="'.$i.'" '.$select.'>'.$i.'</option>';
}
?>
<!DOCTYPE html>
<html>
<head>
	<title>万年历</title>
</head>
<body>
	年<select οnchange="getYear(this.value);">
		<?php echo $yearList;?>
	</select>
	月<select οnchange="getMon(this.value);">
		<?php echo $monList;?>
	</select>
</body>
</html>
<script type="text/javascript">
	var year = <?php echo $year;?>;
	var mon = <?php echo $mon;?>;
	function getYear(val) {
		window.location.href="http://localhost/test/rili.php?y="+val+"&m="+mon;
	}
	function getMon(val) {
		window.location.href="http://localhost/test/rili.php?y="+year+"&m="+val;
	}
</script>


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值