本节主要内容:
一个php实现的日历效果代码。
演示效果:
php日历源码,如下:
复制代码 代码示例:
<?php
function build_calendar($month,$year,$dateArray) {
// 日历表头,星期天开始一直到星期六
$daysOfWeek = array('S','M','T','W','T','F','S');
// 本月第一天的位置
$firstDayOfMonth = mktime(0,0,0,$month,1,$year);
// 获取本月天数
$numberDays = date('t',$firstDayOfMonth);
// 获取本月第一天
$dateComponents = getdate($firstDayOfMonth);
// 获取月份的英文单词
$monthName = $dateComponents['month'];
$dayOfWeek = $dateComponents['wday'];
// 月历表头
$calendar = "<table class='calendar'>";
$calendar .= "<caption>$monthName $year</caption>";
$calendar .= "<tr>";
// 星期表头
foreach($daysOfWeek as $day) {
$calendar .= "<th class='header'>$day</th>";
}
// 开始输出日历
// 初始化天数计数器,从1号开始
$currentDay = 1;
$calendar .= "</tr><tr>";
// 使用变量 $dayOfWeek 可以保证一周七天精确输出
if ($dayOfWeek > 0) {
$calendar .= "<td colspan='$dayOfWeek'> </td>";
}
$month = str_pad($month, 2, "0", STR_PAD_LEFT);
while ($currentDay <= $numberDays) {
// 7天一行,7天一到新增一行
if ($dayOfWeek == 7) {
$dayOfWeek = 0;
$calendar .= "</tr><tr>";
}
$currentDayRel = str_pad($currentDay, 2, "0", STR_PAD_LEFT);
$date = "$year-$month-$currentDayRel";
$calendar .= "<td class='day' rel='$date'>$currentDay</td>";
// 计数器
$currentDay++;
$dayOfWeek++;
}
// 最后一行表格的处理,往往最后一行不可能全部填满,需要要空格填充。
if ($dayOfWeek != 7) {
$remainingDays = 7 - $dayOfWeek;
$calendar .= "<td colspan='$remainingDays'> </td>";
}
$calendar .= "</tr>";
$calendar .= "</table>";
return $calendar;
}
?>
php日历代码的调用示例:
复制代码 代码示例:
<?php
//php日历
$dateComponents = getdate();
$month = $dateComponents['mon'];
$year = $dateComponents['year'];
echo build_calendar($month,$year,$dateArray);
?>
>>> 您可能感兴趣的文章:
php calender日历代码(解决2038问题)
php日历代码分享 简单实用的php日历代码
php日历代码 php实现的高效日历代码
php日历代码 三个不错的php日历代码
分享一例PHP简单日历代码
php简单日历的实现代码(可绑定事件)