本篇文章主要介绍php如何写日历的一个小案例,感兴趣的朋友参考下,希望对大家有所帮助。
代码如下:
<?php
//修改页面编码
header("Content-type: text/html; charset=utf-8");
//如果没有传入年份则获取当前系统年份
$year=!isset($_GET['y'])?$_GET['y']:date('Y');
//如果没有传入月份则获取当前系统月份
$month=$_GET['m']?$_GET['m']:date('m');
//获取当前月有多少天
$days=date('t',strtotime("{$year}-{$month}-1"));
//当前1号是星期几
$week=date('w',strtotime("{$year}-{$month}-1"));
//输出表头
echo "<center>";
echo "<h2>{$year}年{$month}月</h2>";
//输出日期表格
echo "<table width='700px' border='1px'>";
echo "<tr>";
echo "<th>周日</th>";
echo "<th>周一</th>";
echo "<th>周二</th>";
echo "<th>周三</th>";
echo "<th>周四</th>";
echo "<th>周五</th>";
echo "<th>周六</th>";
echo "</tr>";
//铺表格
for($i=1-$week;$i<=$days;){
echo "<tr>";
for($j=0;$j<7;$j++){
if($i>$days || $i<=0){
echo "<td> </td>";
}else{
echo "<td>{$i}</td>";
}
$i++;
}
echo "</tr>";
}
echo "</table>";
//实现上一月和上一年
if($month==1){
$premonth = 12;
$preyear = $year - 1;
}else{
$premonth = $month-1;
$preyear = $year;
}
//实现下一月和下一年
if($month==12){
$nextmonth = 1;
$nextyear = $year + 1;
}else{
$nextmonth = $month + 1;
$nextyear = $year;
}
//上一月、下一月的实现
echo "<a href='http://localhost/index.php?y={$premonth}&m={$premonth}'>上个月</a>";
echo "<a href='http://localhost/index.php?y={$nextmonth}&m={$nextmonth}'>下个月</a>";
echo "</center>";
?>相关推荐:
立即学习“PHP免费学习笔记(深入)”;
以上就是PHP实现日历案例的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号