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

$dateComponents = getdate();
$month = $dateComponents[‘mon’];
$year = $dateComponents[‘year’] ;
echo build_calendar($month,$year,$dateArray);

?>

订阅文章

在下面输入您的电子邮件地址以订阅我们的通讯

留下评论

Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124