>[success] # PHP 實現日歷功能

~~~php
//show_calendar.php
<?php
//設定用于一個腳本中所有日期時間函數的默認時區
if (function_exists('date_default_timezone_set')) {
date_default_timezone_set('PRC');
}
function post_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$year = isset($_GET["y"]) ? post_input($_GET["y"]) : date("Y");
$month = isset($_GET["m"]) ? post_input($_GET["m"]) : date("m");
if ($month > 12) {
$month = 1;
$year++;
} elseif ($month < 0) {
$month = 12;
$year--;
}
$t = strtotime("$year-$month-1");
$last = date('t', strtotime('-1 month', $t));
$start = date('w', $t) ? range($last - date('w', $t) + 1, $last) : range($last - 6, $last);
$days = range(1, date('t', $t));
$end = range(1, 6 * 7 - count($start) - count($days));
$daytable = "<tr>";
$w = 1;
for ($i = 0; $i < count($start); $i++) {
if ($w % 7 == 0) {
$daytable .= "<td data-title='last' onclick='alert(1);'>$start[$i]</td></tr><tr>";
} else {
$daytable .= "<td data-title='last' onclick='alert(1);'>$start[$i]</td>";
}
$w++;
}
for ($i = 0; $i < count($days); $i++) {
if ($w % 7 == 0) {
$daytable .= "<td>$days[$i]</td></tr><tr>";
} else {
$daytable .= "<td>$days[$i]</td>";
}
$w++;
}
for ($i = 0; $i < count($end); $i++) {
if ($w % 7 == 0) {
$daytable .= "<td style='color:#898989' data-title='last' onclick='alert(1);'>$end[$i]</td></tr><tr>";
} else {
$daytable .= "<td style='color:#898989' data-title='last' onclick='alert(1);'>$end[$i]</td>";
}
$w++;
}
$daytable .= "<tr/></tbody></table>";
?>
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
td {
text-align: center;
}
td[data-title='last'] {
color: #898989;
cursor: pointer;
background: #fdf5ce none repeat scroll 0 0;
}
td:hover {
position: relative;
}
td[data-title='last']:hover:before {
content: attr(data-title);
position: absolute;
left: 5px;
top: 100%;
background: #ff8403 none repeat scroll 0 0;
color: #99FFFF;
width: 70px;
height: 30px;
line-height: 30px;
border-radius: 5px;
box-shadow: 3px 3px 3px #666666;
}
</style>
</head>
<body>
<table>
<thead>
<tr align='center'>
<th colspan='1'>
<a href='show_calendar.php?y=<?php echo $year ?>&m=<?php echo $month - 1 ?>'>上一月</a>
</th>
<th colspan='5'><?php echo $year ?>年<?php echo $month ?>月份</th>
<th colspan='1'>
<a href='show_calendar.php?y=<?php echo $year ?>&m=<?php echo $month + 1 ?>'>下一月</a>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>星期日</td>
<td>星期一</td>
<td>星期二</td>
<td>星期三</td>
<td>星期四</td>
<td>星期五</td>
<td>星期六</td>
</tr>
<?php echo $daytable ?>
</tbody>
</table>
</body>
</html>
~~~
- 技術擴展閱讀
- 第一章
- 第一節 PHP與Golang 項目案例 - 留言板
- 第二節 PHP 實現日歷功能
- 第三節 ThinkPHP 自定義分頁模板
- 第四節 WebUpload 文件上傳
- 第五節 UEditor 文本編輯器
- 第六節 ThinkPHP 驗證碼
- 第七節 百度地圖
- 第八節 PHP 接口調試工具 SocketLog
- 第九節 PHP 跟蹤調試代碼 XDebug
- 第十節 PHPExcel 表格導入和導出
- 第二章
- 十一節 實戰筆記 - Kafka 篇
- 十二節 實戰筆記 - Redis 篇
- 十三節 實戰筆記 - MySQL 篇
- 十四節 圖片轉ASCII碼圖
- 十六節 Python 視頻轉代碼視頻
- 源代碼
- 代碼2
- 十七節 GRPC PHP客戶端