最近開發(fā)需求中需要輸出一個(gè)日歷,由于該日歷和數(shù)據(jù)庫中的數(shù)據(jù)扯有太多關(guān)系。于是沒有選擇比較常見的用js生成的日歷,而是用php輸出了一個(gè)日歷表格。ok,下面將如何用php輸出日歷的程序和大家分享一下。
php輸出的日歷效果如下圖:
具體代碼如下:
<p><?php</p>/*<br />* php 輸出日歷程序<br />*/ <br />header("Content-type: text/html;charset=utf-8");<br />$year=(!isset($_GET['year'])||$_GET['year']=="")?date("Y"):$_GET['year'];<br />$month=(!isset($_GET['month'])||$_GET['month']=="")?date("n"):$_GET['month'];<br />$curUrl=$_SERVER['PHP_SELF'];<br /><br />if($year<1971){<br /> echo "出錯(cuò)!";<br /> echo "<BR>";<br /> echo "<a href=",$curUrl,">Back</a>";<br /> exit();<br />}<br />?> <br /><table width="200" border="1" cellspacing="0" cellpadding="0" bordercolor="#E7E7E7" style="font-size:12px;" align="center"> <br /><tr align="center"><td colspan="2"> <br /><?php <br />//<-------當(dāng)月份超出1至12時(shí)的處理;開始-------> <br />if($month<1){<br /> $month=12;<br /> $year-=1;<br />}<br />if($month>12){<br /> $month=1;<br /> $year+=1;<br />}<br />//<-------當(dāng)月份超出1至12時(shí)的處理;結(jié)束-------> <br />//<---------上一年,下一年,上月,下月的連接處理及輸出;開始---------> <br />echo "<a href=",$curUrl,"?year=".($year-1)."&month=".$month."><<</a>年<a href=",$curUrl,"?year=".($year+1)."&month=".$month.">>></a>";<br />?> <br /></td><td colspan="3"><?php echo $year."年".$month."月";?> <br /></td><td colspan="2"> <br /><?php <br />echo "<a href=",$curUrl,"?month=".($month-1)."&year=".$year."><<</a>月<a href=",$curUrl,"?month=".($month+1)."&year=".$year.">>></a>";<br />//<--------上一年,下一年,上月,下月的連接處理及輸出;結(jié)束---------> <br />?></td></tr> <br /><tr align=center><td><font color="red">日</font></td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr><tr> <br /><?php <br />$d=date("d");<br />$FirstDay=date("w",mktime(0,0,0,$month,1,$year));//取得任何一個(gè)月的一號(hào)是星期幾,用于計(jì)算一號(hào)是由表格的第幾格開始<br />$bgtoday=date("d");<br />function font_color($month,$today,$year){//用于計(jì)算星期天的字體顏色<br /> $sunday=date("w",mktime(0,0,0,$month,$today,$year));<br /> if($sunday=="0"){<br /> $FontColor="red";<br /> }else{<br /> $FontColor="black";<br /> }<br /> return $FontColor;<br />}<br />function bgcolor($month,$bgtoday,$today_i,$year){//用于計(jì)算當(dāng)日的背景顏色<br /> $show_today=date("d",mktime(0,0,0,$month,$today_i,$year));<br /> $sys_today=date("d",mktime(0,0,0,$month,$bgtoday,$year));<br /> if($show_today==$sys_today){<br /> $bgcolor="bgcolor=#6699FF";<br /> }else{<br /> $bgcolor="";<br /> }<br /> return $bgcolor;<br />}<br />function font_style($month,$today,$year){//用于計(jì)算星期天的字體風(fēng)格<br /> $sunday=date("w",mktime(0,0,0,$month,$today,$year));<br /> if($sunday=="0"){<br /> $FontStyle="<strong>";<br /> }else{<br /> $Font;<br /> }<br /> return $FontStyle;<br />}<br />for($i=0;$i<=$FirstDay;$i++){//此for用于輸出某個(gè)月的一號(hào)位置<br /> for($i;$i<$FirstDay;$i++){<br /> echo "<td align=center> </td>\n";<br /> }<br /> if($i==$FirstDay){<br /> echo "<td align=center ".bgcolor($month,$bgtoday,1,$year)."><font color=".font_color($month,1,$year).">".font_style($month,1,$year)."1</font></td>\n";<br /> if($FirstDay==6){//判斷1號(hào)是否星期六<br /> echo "</tr>";<br /> }<br /> }<br />}<br />$countMonth=date("t",mktime(0,0,0,$month,1,$year));//某月的總天數(shù) <br />for($i=2;$i<=$countMonth;$i++){//輸出由1號(hào)定位,隨后2號(hào)直至月尾的所有號(hào)數(shù)<br /> echo "<td align=center ".bgcolor($month,$bgtoday,$i,$year)."><font color=".font_color($month,$i,$year).">".font_style($month,$i,$year)."$i</font></td>\n";<br /> if(date("w",mktime(0,0,0,$month,$i,$year))==6){//判斷該日是否星期六<br /> echo "</tr>\n";<br /> }<br />}<br />?> <br /></table>

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

TostaycurrentwithPHPdevelopmentsandbestpractices,followkeynewssourceslikePHP.netandPHPWeekly,engagewithcommunitiesonforumsandconferences,keeptoolingupdatedandgraduallyadoptnewfeatures,andreadorcontributetoopensourceprojects.First,followreliablesource

PHPbecamepopularforwebdevelopmentduetoitseaseoflearning,seamlessintegrationwithHTML,widespreadhostingsupport,andalargeecosystemincludingframeworkslikeLaravelandCMSplatformslikeWordPress.Itexcelsinhandlingformsubmissions,managingusersessions,interacti

TosettherighttimezoneinPHP,usedate_default_timezone_set()functionatthestartofyourscriptwithavalididentifiersuchas'America/New_York'.1.Usedate_default_timezone_set()beforeanydate/timefunctions.2.Alternatively,configurethephp.inifilebysettingdate.timez

TovalidateuserinputinPHP,usebuilt-invalidationfunctionslikefilter_var()andfilter_input(),applyregularexpressionsforcustomformatssuchasusernamesorphonenumbers,checkdatatypesfornumericvalueslikeageorprice,setlengthlimitsandtrimwhitespacetopreventlayout

The key to writing clean and easy-to-maintain PHP code lies in clear naming, following standards, reasonable structure, making good use of comments and testability. 1. Use clear variables, functions and class names, such as $userData and calculateTotalPrice(); 2. Follow the PSR-12 standard unified code style; 3. Split the code structure according to responsibilities, and organize it using MVC or Laravel-style catalogs; 4. Avoid noodles-style code and split the logic into small functions with a single responsibility; 5. Add comments at key points and write interface documents to clarify parameters, return values ??and exceptions; 6. Improve testability, adopt dependency injection, reduce global state and static methods. These practices improve code quality, collaboration efficiency and post-maintenance ease.

ThePhpfunctionSerialize () andunserialize () AreusedtoconvertcomplexdaTastructdestoresintostoraSandaBackagain.1.Serialize () c OnvertsdatalikecarraysorobjectsraystringcontainingTypeandstructureinformation.2.unserialize () Reconstruct theoriginalatataprom

You can embed PHP code into HTML files, but make sure that the file has an extension of .php so that the server can parse it correctly. Use standard tags to wrap PHP code, insert dynamic content anywhere in HTML. In addition, you can switch PHP and HTML multiple times in the same file to realize dynamic functions such as conditional rendering. Be sure to pay attention to the server configuration and syntax correctness to avoid problems caused by short labels, quotation mark errors or omitted end labels.

Yes,youcanrunSQLqueriesusingPHP,andtheprocessinvolveschoosingadatabaseextension,connectingtothedatabase,executingqueriessafely,andclosingconnectionswhendone.Todothis,firstchoosebetweenMySQLiorPDO,withPDObeingmoreflexibleduetosupportingmultipledatabas
