以下代码只是简单实现日历的效果和逻辑思路,没有使用类封装,权当抛砖引玉,有兴趣的朋友可以封装起来,方便调用。
<?php /**
* PHP利用时间函数输出日历
* Rain.zen $ intval@163.com
*/ /**
* 获取关于日历的格式对应的天数
* @param $format 获取的格式[t:指定月份的天数; w:数字型的星期数(0-星期日,6-星期六)]
* @param $year 年份,例:2009
* @param $month 月份,例:03
* @param $day 日期,例:08
* @return bool|string 返回值是根据格式$format的选择决定
*/
function calenderNum($format, $year, $month, $day){
return date($format, mktime(0, 0, 0, $month, $day, $year));
} // 月份数组
$aryMonth = array('一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二'); // 当前年月日
$thenTime = date('Y-m-d');
list($thenYear, $thenMonth, $thenDay) = explode('-', $thenTime); // 是否是星期日作为开头[1:是; 0:否]
$isSunday = 0; // 日期:年份下拉控件
$year = isset($_POST['year']) ? $_POST['year'] : $thenYear;
$yearLast = 8; // 年份的前后持续周期
$selectYear = '<select name="year" onchange="this.form.submit();">';
for($i = ($year + $yearLast), $n = ($year - $yearLast); $i >= $n; $i--){
$selected = $i == $year ? ' selected="selected"' : '';
$selectYear.= '<option value="'.$i.'"'.$selected.'>'.$i.'</option>';
}
$selectYear.= '</select>'; // 日期:月份下拉控件
$month = isset($_POST['month']) ? $_POST['month'] : $thenMonth;
$selectMonth = '<select name="month" onchange="this.form.submit();">';
foreach($aryMonth as $k => $v){
$k = sprintf('%02d', ($k + 1));
$selected = $k == $month ? ' selected="selected"' : '';
$selectMonth.= '<option value="'.$k.'"'.$selected.'>'.$v.'月</option>';
}
$selectMonth.= '</select>';
unset($k, $v); // 日期:星期HTML
$aryWeek = array('一', '二', '三', '四', '五', '六');
$isSunday AND array_unshift($aryWeek, '日');
$isSunday OR array_push($aryWeek, '日');
$weekTrHtml = '<tr>';
foreach($aryWeek as $v){
$weekTrHtml.= '<td>周'.$v.'</td>';
}
$weekTrHtml.= '</tr>';
unset($v); // 以下为计算当月的天数以及前后月份作为空位补齐日历表格
$allDay = calenderNum('t', $year, $month, $thenDay); // 指定月份的天数
$monthFirstDay2weekVal = calenderNum('w', $year, $month, 1); // 指定日期的星期数
$aryDay = range(1, $allDay); // 计算第一排表格空位数以及是否需要补齐
$each = $isSunday ? 7 : 6;
$firstNum = $isSunday ? $monthFirstDay2weekVal : ($monthFirstDay2weekVal-1);
$isAddDay = $firstNum % $each; // 用上月的最后几天补齐空位数
if($isAddDay != 0 ){
$prevMonth = intval($month) - 1;
$prevMonthDay = calenderNum('t', $year, $prevMonth, 1); // 计算上月的总天数
$aryFirst = range($prevMonthDay - $firstNum + 1, $prevMonthDay);
$aryDay = array_merge($aryFirst, $aryDay);
} // 计算最后一排表格空位数以及是否需要补齐
$lastNum = count($aryDay) % 7;
$lastNum == 0 OR $aryLast = range(1, (7 - $lastNum));
isset($aryLast) AND $aryDay = array_merge($aryDay, $aryLast); // 循环输出日期的表格
$x = count($aryDay) / 7;
$y = 1;
$dayTrHtml = '<tr>';
foreach($aryDay as $i => $rsDay){ $strDay = str_pad($rsDay,2,'0',STR_PAD_LEFT);
$dayOass = ($i < $firstNum || $i >= $firstNum + $allDay) ? ' class="z-pass"' : '' ; // 把非本月的日期变为灰色
$dayOn = ($thenYear == $year && $thenMonth == $month && $strDay == $thenDay && empty($dayOass)) ? ' class="z-on"' : ''; // 对当前的日期加亮显示 $dayTrHtml.= '<td'.$dayOn.$dayOass.'>'.$strDay.'</td>';
if(($i + 1) % 7 == 0){
$x == $y OR $dayTrHtml.= '</tr>'.'<tr>';
$y++;
}
}
$dayTrHtml.= '</tr>';
?>
模板HTML代码
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<title>PHP程序输出日历</title>
<style type="text/css">
/* global css document => author:intval@163.com */
html{height:100%;-overflow-y:scroll;border:0}body{min-height:100%;-height:100%;position:relative}
body,h1,h2,h3,h4,h5,h6,p,div,dl,dt,dd,ul,ol,li,form,button,input,textarea,th,td{margin:0;padding:0;border:0}
h1,h2,h3,h4,h5,h6,select,input,textarea,button,table{font-size:100%;font-weight:normal}
ul,ol,dl{list-style:none}a{text-decoration:none;color:#36c}u{text-decoration:none}i,em{font-style:normal}
a:hover{text-decoration:underline}a img{border:none}a,select,input,textarea{outline:none}.fr{float:right}
table{border-collapse:collapse;border-spacing:0}th{text-align:left;font-weight:normal}.fl{float:left}
.cf:before,.cf:after{content:"";display:table}.cf:after{clear:both}.cf{+zoom:1}
body{color:#000;background:#fff;font:12px/15px Microsoft YaHei} #u-calender{padding:50px 0 0 200px}
#u-calender #u-slt{font-size:14px}
#u-slt select{font:13px Verdana}
#u-calender .z-on{background:#36c;color:#fff; font-weight:700}
#u-calender .z-pass{background:#efefef;color:#ccc}
#u-calender table{ border-top:1px solid #ccc; border-right:1px solid #ccc; width:300px}
#u-calender table tr td{ border-bottom:1px solid #ccc; border-left:1px solid #ccc;text-align:center;padding:5px 0}
</style>
</head> <body>
<div id="u-calender">
<table><tr><td colspan="7" id="u-slt"><form action="" method="post">月份:<?php echo $selectMonth;?> 年份:<?php echo $selectYear;?></form></td></tr>
<?php echo $weekTrHtml.$dayTrHtml;?>
</table>
</div>
</body>
</html>
代码完成后实现的效果截图: