PHP自动生成月历代码

 1   
 2/*   
 3Function Written by Nelson Neoh @3/2004.   
 4For those who wants to utilize this code, please do not remove this remark.   
 5If you have done any enhancement to this code, please post the copy at http://www.dev-club.com PHP board. Thank you. 
 6
 7Function usage: calendar(Month,Year)   
 8*/   
 9  
10function calendar($MM,$YYYY){   
11if($MM=="") $MM = date("m");   
12if($YYYY=="") $YYYY = date("Y");   
13if(checkdate($MM,1,$YYYY)){   
14$stringDate = strftime("%d %b %Y",mktime (0,0,0,$MM,1,$YYYY));   
15$days = strftime("%d",mktime (0,0,0,$MM+1,0,$YYYY));   
16$firstDay = strftime("%w",mktime (0,0,0,$MM,1,$YYYY));   
17$lastDay = strftime("%w",mktime (0,0,0,$MM,$days,$YYYY));   
18$printDays = $days;   
19$preMonth = strftime("%m",mktime (0,0,0,$MM-1,1,$YYYY));   
20$preYear = strftime("%Y",mktime (0,0,0,$MM-1,1,$YYYY));   
21$nextMonth = strftime("%m",mktime (0,0,0,$MM+1,1,$YYYY));   
22$nextYear = strftime("%Y",mktime (0,0,0,$MM+1,1,$YYYY));   
23print("

<table border='"1"' cellpadding='"1"' cellspacing='"1"'>");
print("<tr><th valign='"top"'><a href="&amp;quot;&quot;.$_SERVER['PHP_SELF'].&quot;?NB=&quot;.$_GET[&quot;NB&quot;].&quot;&amp;MM=&quot;.$preMonth.&quot;&amp;YY=&quot;.$preYear.&quot;&amp;quot;">P</a></th>");
print("<th colspan='"5"' valign='"top"'>".strftime("%b %Y",mktime (0,0,0,$MM,1,$YYYY))."</th>");
print("<th valign='"top"'><a href="&amp;quot;&quot;.$_SERVER['PHP_SELF'].&quot;?NB=&quot;.$_GET[&quot;NB&quot;].&quot;&amp;MM=&quot;.$nextMonth.&quot;&amp;YY=&quot;.$nextYear.&quot;&amp;quot;">N</a></th></tr>");
print("<tr font-size:x-small"="" style='"font-family:' verdana;="">");
print("<th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>");

$currentDays = 1;
for($a=1;$a&lt;=5;$a++){
print("<tr align='"left"' font-size:x-small"="" style='"font-family:' valign='"top"' verdana;="">");
$diffDays = $firstDay-$lastDay;
if($firstDay&gt;$lastDay &amp;&amp; $currentDays ==1 &amp;&amp; ($diffDays&lt;&gt;1)){
for($x=$lastDay;$x&gt;=0;$x--){
$printDays = $days-$x;
print("<td>$printDays</td>");
}
for($z=1;$z&lt;$firstDay-$lastDay;$z++){
print("<td> </td>");
}
for($y=$firstDay;$y&lt;7;$y++){
print("<td>$currentDays</td>");
$currentDays++;
}
} elseif($firstDay!=0 &amp;&amp; $currentDays==1){
for($z=1;$z&lt;=$firstDay;$z++){
print("<td> </td>");
}
for($y=$firstDay;$y&lt;7;$y++){
print("<td>$currentDays</td>");
$currentDays++;
}
} else {
for($u=1;$u&lt;=7 &amp;&amp; $currentDays&lt;=$days;$u++){
print("<td>$currentDays</td>");
$currentDays++;
}
}
print("</tr>");
}
print("</table>

1");   
2}   
3}   
Published At
Categories with Web编程
Tagged with
comments powered by Disqus