<?php
$weekArr = array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');//获取空格:
//echo date("D",strtotime("2012-02-07")); function getSpace($month,$year=''){ global $weekArr; if(!$year){ $year = date("Y"); } $firstDay = date("D",strtotime($year."-".$month."-01"));$weekArrNew = array_flip($weekArr);
$space = $weekArrNew[$firstDay]; return $space; }//获取当月有多少天:
function getMonthDays($month,$year=''){ if(!$year){ $year = date("Y"); } for($i=28;$i<=32;$i++){ if(!checkdate($month,$i,$year)){ return $i-1; break; } } }//打印日历:
function printRili($month,$year=''){ global $weekArr; if(!$year){ $year = date("Y"); } $space = getSpace($month,$year); for($i=1;$i<=$space;$i++){ $weekArr[] = ' '; } $monthDays = getMonthDays($month,$year); for($i=1;$i<=$monthDays;$i++){ $weekArr[] = $i; } //print_r($weekArr); $returnStr = '<table><tr class=firststr>'; foreach($weekArr as $key=>$val){ if((($key+1) % 7 == 0) && $key!=0){ $returnStr .= '<td>'.$val.'</td></tr><tr class=strnum>'; }else{ $returnStr.='<td>'.$val.'</td>'; } } $returnStr .= '</tr></table>'; return $returnStr; }//打印2012年3月份的日历:
echo printRili(3); ?> <style> td{ text-align:center; } .firststr{ background:orange; font-weight:bold; } .strnum{ background:gray; color:white; } </style>