用PHP实现万年历功能的要点:
获取当月要处理的总天数$days获取当月第一天要处理的天数$dayofweek$days Function :知道一个月中有多少天要处理,就可以循环输出天数。
$dayofweek :的函数只有知道每月1日的星期几,才能知道在输出日之前需要输出多少空格(空格)。
最终渲染如下:
“万年历类”的代码如下:
复制代码如下:Php/*** PHP万年历* @ authorfly 2012/10/16 */class calendar { protected $ _ table;//table table protected $ _ current date;//当前日期受$_year保护;//年受保护$ _月;//月保护$ _天;//给定月份的天数应受保护$ _ dayofweek//给定月份的第一天是哪天/* * * constructor */public function _ _ construct(){ $ this-_ table=' ';$this-_year=isset($_GET['y'])?$ _ GET[' Y ']: date(' Y ');$this-_month=isset($_GET['m'])?$ _ GET[' m ']: date(' m ');If ($this-_month12){//处理月份大于12 $this-_month=1的情况;$今年-_年;} if ($this-_month1){//当月份小于1时,$ this-_ month=12;$今年-_年-;} $ this-_ current date=$ this-_ year。一年。$本月-_。月';//目前获取的日期信息为$ this-_ days=date ('t ',mktime (0,0,0,$ this-_ month,1,$ this-_ year));//获取给定月份的天数$ this-_ dayofweek=date ('w ',mktime (0,0,0,$ this-_ month,1,$ this-_ year));//获取给定月份第一天的星期几}/* * *输出标题和标题信息*/protected function _ show title(){ $ this-_ table=' tablet head tr align=' center ' th col span=' 7 '。$ this-_ currentdate。/th/tr/the ad ';$ this-_表。=' tbodytr$ this-_表。=' TD style=' color : red ' Sunday/TD ';$ this-_表。='td星期一/TD ';$ this-_表。=' TD星期二/TD ';$ this-_表。=' TD星期三/TD ';$ this-_表。='td星期四/TD ';$ this-_表。=' TD Friday/TD ';$ this-_表。=' TD style=' color:red '星期六/TD ';$ this-_表。='/tr ';}/* * *输出日期信息*根据当前日期输出日期信息*/Protected function _ show date(){ $ nums=$ this-_ dayofweek1;for($ I=1;$ I=$ this-_ dayofweek;$i ){//输出1号$ this-_表前的空白日期。=' TD nbsp/TD ';} for($ I=1;$ I=$ this-_ days;$i ){//输出天数信息if ($nums%7==0){//换行:7行$this-_table。=' TD $ I/TD/trtr ';}else{ $this-_table。=' TD $ I/TD ';} $ nums} $this-_table。='/t body/table ';$ this-_表。='h3a href='?Y='。($今年-_年)。m='。($ this-_ month-1)。“上个月/a”;$ this-_表。='a href='?Y='。($今年-_年)。m='。($ this-_ month1)。“下个月/a/H3”;}/* * *输出日历*/公共函数show calendar(){ $ this-_ show title();$ this-_ show date();echo $ this-_ table;} } $ calc=新日历();$ calc-showCalendar();
效果还不错,朋友可以美化自己,这里就不解释了。