时间函数实现万年历

<?php
$time=time();

//计算当前年
$year=$_GET['y']?$_GET['y']:date('Y',$time);

//计算当前月
$month=$_GET['m']?$_GET['m']:date('m',$time);

//当前月总计多少天?
$days=date('t',strtotime("{$year}-{$month}-1"));

//当前月的第一天是周几?
$week=date('w',strtotime("{$year}-{$month}-1"));

//计算万年历格中第一天的数字
$first=1-$week;

//上一月和上一年
$prevMonth=$month-1;
$prevYear=$year;
if($prevMonth<1){
$prevMonth=12;
$prevYear=$year-1;
}

//下一月和下一年
$nextMonth=$month+1;
$nextYear=$year;
if($nextMonth>12){
$nextMonth=1;
$nextYear=$year+1;
}

?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
<style>
*{
font-family: 微软雅黑;
}

a{
text-decoration: none;
color:#00f;
}
</style>
</head>
<body>
<center>
<?php
echo "<h2>万年历-{$year}年{$month}月</h2>";
?>

<table width='700px' border='1px' cellspacing='0'>
<tr>
<th>周日</th>
<th>周一</th>
<th>周二</th>
<th>周三</th>
<th>周四</th>
<th>周五</th>
<th>周六</th>
</tr>
<?php
//布局万年历的表格
for($i=$first;$i<=$days;){
echo '<tr>';
for($j=0;$j<7;$j++){
if($i<=$days && $i>=1){
echo "<td>{$i}</td>";
}else{
echo '<td>&nbsp;</td>';
}
$i++;
}
echo '</tr>';
}
?>
</table>

<h3>
<a href="index.php?y=<?php echo $prevYear?>&m=<?php echo $prevMonth ?>">上一月</a> |
<a href="index.php?y=<?php echo $nextYear?>&m=<?php echo $nextMonth ?>">下一月</a>
</h3>
</center>
</body>
</html>

posted @   前端乔  阅读(297)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
点击右上角即可分享
微信分享提示