Home > php教程 > PHP源码 > PHP手写日历代码

PHP手写日历代码

PHP中文网
Release: 2016-05-25 17:14:12
Original
1110 people have browsed it

[PHP]代码   

$monthnow=isset($_GET['mon'])?$_GET['mon']:date("n");
$yearnow=isset($_GET['year'])?$_GET['year']:date("Y");
if($monthnow==13){
	$monthnow=1;
	$yearnow=$yearnow+1;
}
if($monthnow==0){
	$monthnow=12;
	$yearnow=$yearnow-1;
}
 
$mdays=date("t");    //当月总天数
$datenow=date("j");  //当日日期
 
//计算当月第一天是星期几
$wk1st=date("w",mktime(0,0,0,$monthnow,1,$yearnow));
 
$trnum=ceil(($mdays+$wk1st)/7); //计算表格行数
 
//表格字串
$tabstr="
<!--
a{TEXT-DECORATION:none;color:#000;}  tr,td{border=&#39;1&#39; bordercolor=&#39;#fff&#39;}
-->
<table style="border-collapse: collapse; height: 250px;" border="1" width="300" align="center" bordercolor="#46a3ff">";
$tabstr.="
<tbody>
<tr>
<td><a href="?mon=&quot;.($monthnow-1).&quot;&amp;year=&quot;.$yearnow.&quot;">&lt;&lt;&lt;</a></td>
<td colspan="5" align="center">"; $tabstr.=$yearnow.&#39;年  &#39;.$monthnow.&#39;月  &#39;.date(&#39;d&#39;,time())."日  </td>
<td>"; $tabstr.="<a href="?mon=&quot;.($monthnow+1).&quot;&amp;year=&quot;.$yearnow.&quot;">&gt;&gt;&gt;</a></td>
</tr>
";
$tabstr.="
<tr>
<td align="center">日</td>
<td align="center">一</td>
<td align="center">二</td>
<td align="center">三</td>
<td align="center">四</td>
<td align="center">五</td>
<td align="center">六</td>
</tr>
";
 
for($i=0;$i&lt;$trnum;$i++) {
   $tabstr.="
<tr>";
   //每行七个单元格
   for($k=0;$k&lt;7;$k++) {
   	  //取得单元格自身序号
   	  $tabidx=$i*7+$k;
      //若单元格序号小于当月第一天的星期数($wk1st)或大于(月总数+$wk1st),只填写空格,反之,写入日期
      ($tabidx&lt;$wk1st or $tabidx&gt;$mdays+$wk1st-1) ? $dayecho="&amp;nbsp" : $dayecho=$tabidx-$wk1st+1;
      //突出标明今日日期
      if($dayecho!=&#39;&amp;nbsp&#39;){
      		$num=$dayecho;
      		if($dayecho==$datenow) $dayecho="<span style="background-color: #185096; color: #fff;"><strong>".$dayecho."</strong></span>";
      		$tabstr.=&#39;
<td align="center"><a href="#&amp;m="></a></td>
";
      }else{
      		$tabstr.=&#39;
<td align="center">&#39;.$dayecho."</td>
";
      }
   }
   $tabstr.="</tr>
";
}
$tabstr.="</tbody>
</table>
";
 
print $tabstr; //输出日历
Copy after login

                   

                   

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template