PHP练手:日历(代码简单,扩展容易)
抽空写了个日历程序,只注重功能和实现的思路,所以代码和功能都比较简单,但是理解和扩展也比较容易。
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;$ifirstNum);$i++){ echo '<td>'; if($i >= $this->firstNum+1){ $a = $i-$this->firstNum; if($a == $day){ echo '<div style="background-color:blue;color:#fff;">'.$a.'</div>'; }else{ echo '<div>'.$a.'</div>'; } } echo '</td>'; if($i%7 == 0){//每输出7列就换一行 echo '</tr>'; } } echo ''; } } $calendar = new Calendar(); $calendar->show();
效果图:
show()函数用来显示日历,你可以修改show()函数,通过传值的方式来实现显示不同的年月。
扩展:
本文的主要知识点在于PHP date()函数的使用
这里有篇date()函数的详解:http://wenku.baidu.com/link?url=OPDGzaCMWgjDE0ya8QlDbLIXX0c11ohUjsoLyRm-cYp7lz-O_7H4XBILv8JfomhbnSxXSW0FhqxYBK0_gn8Nr77XMWp-_st7v8AYecbNZjG

Thesecrettokeepingaphp-betriebene WebsiterunningSmoothyunderheavyLoadInvolvesseveralkeyStrategies: 1) ImplementoPCodeCachingWithopcachetoreducescholexexexcutiontime, 2) verwendetatabasequerycachingwithredolesendatabaSelaDaLoadaLoadaLoadaLoad, 3) LeveragecdnslikecloudLesendatabaselaSelaSelaSelaSelaSelaSelaSelaSelaSelaSeladinaSelaSelaSelaSelaSeladinaSelaSeladin

Sie sollten sich um die Abhängigkeitsinjektion (DI) kümmern, da Ihr Code klarer und leichter zu warten ist. 1) DI macht es modularer durch Entkopplung von Klassen, 2) verbessert die Bequemlichkeit von Tests und Code -Flexibilität, 3) DI -Container verwenden, um komplexe Abhängigkeiten zu verwalten, aber auf die Auswirkungen auf die Leistung und die kreisförmigen Abhängigkeiten zu achten, 4) Die beste Praxis besteht darin, sich auf abstrakte Schnittstellen zu verlassen, um lose Koupleln zu erreichen.

Ja, optimizingaphpapplicationSispossiblandinential.1) ImplementCachingusedapcutoredatabaSeload.2) optimizedatabases-withindexing, effizienteQuerien und AnconnectionPooling.3) EnhanceCodewithbuilt-Infunktionen, Vermeidung von Globalvariablungen und UsusepcodeCodeCecess

TheKeyStrategieS significantBoostPhpapplicationPlicationperformanceare: 1) UseOpCodeCaching-likeopcachetoreduceExecutiontime, 2) optimizedatabaseInteractionswithprepararedStatements undProperIndexing, 3) configureWebserverSLIKENGINXWITHPHP-FPMFRMFRETBETTERPERSPRIGUNG, 4), 4), 4), 4), 4))

APHPDependencyInjectionContainerisatoolthatmanagesClass -Abhängigkeiten, EnhancingCodemodularität, Testbarkeit und Maschinenbarkeit.

Wählen Sie die Abhängigkeitsinjektion (DI) für große Anwendungen. Der Servicelocator ist für kleine Projekte oder Prototypen geeignet. 1) DI verbessert die Testbarkeit und Modularität des Codes durch Konstruktorinjektion. 2) Servicelocator erhält Dienstleistungen durch die Zentrumregistrierung, was bequem ist, aber zu einer Erhöhung der Codekupplung führen kann.

PhpapplicationscanbeoptimizedforspeedandefficiencyBy: 1) EnabgingOpcacheinphp.ini, 2) usePreparedStatementsWithpdoFordatabasequeries, 3) Ersatzloopswitharray_Filterandarray_mapfordataprozessing, 4) Konfigurieren von), 4), implementieren, 5)

PhpemailvalidationInvolvesthreesteps: 1) Formatvalidationusing -RegularexpressionStocheckTheemailformat; 2) DnsvalidationToensurethedomainhasavalidmxRecord;


Heiße KI -Werkzeuge

Undresser.AI Undress
KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover
Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool
Ausziehbilder kostenlos

Clothoff.io
KI-Kleiderentferner

Video Face Swap
Tauschen Sie Gesichter in jedem Video mühelos mit unserem völlig kostenlosen KI-Gesichtstausch-Tool aus!

Heißer Artikel

Heiße Werkzeuge

SublimeText3 Linux neue Version
SublimeText3 Linux neueste Version

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

Dreamweaver CS6
Visuelle Webentwicklungstools

VSCode Windows 64-Bit-Download
Ein kostenloser und leistungsstarker IDE-Editor von Microsoft

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)
