抽空写了个日历程序,只注重功能和实现的思路,所以代码和功能都比较简单,但是理解和扩展也比较容易。
show()函数用来显示日历,你可以修改show()函数,通过传值的方式来实现显示不同的年月。
<?php class Calendar{ public $weekarray = array('星期日','星期一','星期二','星期三','星期四','星期五','星期六'); public $firstDay = '';//当月第一天 public $firstNum = '';//返回当月第一天对应的星期数字 public $firstDayNum = '';//当月第一天对应的中文星期 /** * 指定日是星期几 * eg:$date='2014-07-21' */ public function getWeek($date){ $date = strtotime($date);//strtotime() 函数将任何英文文本的日期时间描述解析为 Unix 时间戳 $num = date('w',$date);//数字型的星期几,如: "0" (星期日) 至 "6" (星期六) return $num; } /** * 取得指定月有多少天 * eg:$month = '2014-07' */ public function getMonthNum($month){ $month = strtotime($month); return date('t',$month);//指定月份的天数 } /** * 显示日历 */ public function show(){ //取得当前日期 $year = date('Y'); $month = date('m'); $day = date('d'); echo '<table border="1" style="text-align:center;">'; echo '<tr><td colspan="7">'.$year.'-'.$month.'</td></tr>'; echo '<tr><th>星期日</th><th>星期一</th><th>星期二</th><th>星期三</th><th>星期四</th><th>星期五</th><th>星期六</th></tr>'; //取得当前月有多少天 $yearMonth = '$year."-".$month'; $monthNum = $this->getMonthNum($yearMonth); //取得当前月第一天是星期几 $this->firstDay = $year."-".$month."-01"; $this->firstNum = $this->getWeek($this->firstDay); $this->firstDayNum = $this->weekarray[$this->firstNum]; echo '<tr>'; for($i=1;$i<=$monthNum+($this->firstNum);$i++){ echo '<td>'; if($i >= $this->firstNum+1){ $a = $i-$this->firstNum; if($a == $day){ echo '<p style="background-color:blue;color:#fff;">'.$a.'</p>'; }else{ echo '<p>'.$a.'</p>'; } } echo '</td>'; if($i%7 == 0){//每输出7列就换一行 echo '</tr>'; } } echo '</table>'; } } $calendar = new Calendar(); $calendar->show();
效果图:
PHP date()函数的使用
这里有篇date()函数的详解:http://wenku.baidu.com/link?url=OPDGzaCMWgjDE0ya8QlDbLIXX0c11ohUjsoLyRm-cYp7lz-O_7H4XBILv8JfomhbnSxXSW0FhqxYBK0_gn8Nr77XMWp-_st7v8AYecbNZjG

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download
The most popular open source editor

SublimeText3 Chinese version
Chinese version, very easy to use

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
