您的位置:首页 > 编程语言 > PHP开发

使用PHP制作一个万年历

2017-07-02 11:28 489 查看
主要运用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 onchange="getYear(this.value);">
<?php echo $yearList;?>
</select>
月<select onchange="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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: