Calender.php: 给网站应用加上日历归档/导航
📌 TLDR
给网站应用加日历归档/导航:试了几套 PHP 日历库后选定 PHP Calendar 类 —— 支持月/年视图、可定制每个日期链接的地址;写个 MyCalendar 子类重写 getDateLink(),就能让每天链到 /digest/yyyymmdd.html。好处是读者不用手输日期、报表能按天导航,历史归档也更容易被蜘蛛收录。
前2天尝试awstats的按天归档机制,发现忽然需要给统计报表应用做一个日历,每天链接到报表的URL。在网上搜了几套PHP的日历库:发现PHP Calendar是一个最方便的可扩展PHP类包,我需要的2个功能都非常容易方便的定制功能都有了: 1 按月/按年的日历展示; 2 可以定制日期链接到指定的URL地址;
一个简单的月历输出样例在这里:
include '[Calendar.php](http://www.cascade.org.uk/software/php/calendar/Calendar.txt)';
$today = date('j');
// Customize the date links to digest/yyyymmdd.html
class MyCalendar extends Calendar {
function getDateLink($day, $month, $year) {
global $today;
$link = "";
if ( $day < $today ) {
**$link = "/digest/" . date("Ymd", mktime(0, 0, 0, $month, $day, $year)) . ".html";**
}
return $link;
}
}
$cal = new MyCalendar;
// First, create an array of month names, January through December
$chineseMonths = array("一月", "二月", "三月", "四月",
"五月", "六月", "七月", "八月", "九月",
"十月", "十一月", "十二月");
// Then an array of day names, starting with Sunday
$chineseDays = array ("日", "一", "二", "三", "四", "五", "六");
$cal->setMonthNames($chineseMonths);
$cal->setDayNames($chineseDays);
// Week start from Monday
$cal->setStartDay(1);
echo $cal->getCurrentMonthView();
CSS配置:
.calendarHeader {
font-weight: bolder;
color: #CC0000;
background-color: #FFFFCC;
}
.calendarToday {
background-color: #FFFFFF;
}
.calendar {
background-color: #FFFFCC;
}
输出: 1 首页的右上角,给我之前做的Lilina归档器的输出做了一个本月的归档日历; 2 给从2005年5月3日以来的归档做了一个按年的浏览页,PHP源代码在这里:http://www.chedong.com/digest/index.php/source
其实很多日志统计都是按天输出的文本: xxx_yyyymmdd.txt 利用日历归档机制可以很方便的实现导航留言,也不用报表阅读者手工输入日期了。
这个归档页能帮助用户更方便的查找以前的信息外也能更好的帮助搜索引擎的蜘蛛更好的收录: 1 Yahoo 1092条: 提升空间不大 2 Google 369条 3 百度 654条
看看2个月后的收录效果。
💬 历史评论
以下 1 条评论从旧版 Movable Type 博客迁移而来。 原始评论时间:2002–2016 年。
segagoon · 2007-04-13 09:56
大侠,最近搞毕业设计,也准备搞个类似日历的排班系统。到时候有不懂就向您请教了!