PHP自动生成月历代码

  • A+
所属分类:PHP编程

PHP自动生成月历代码

  1. <?php
  2. /*  
  3. Function Written by Nelson Neoh @3/2004.  
  4. For those who wants to utilize this code, please do not remove this remark.  
  5. If you have done any enhancement to this code, please post the copy at http://www.dev-club.com PHP board.  Thank you.
  6. Function usage: calendar(Month,Year)
  7. */
  8. function calendar($MM,$YYYY){
  9.     if($MM=="") $MM = date("m");
  10.     if($YYYY=="") $YYYY = date("Y");
  11.     if(checkdate($MM,1,$YYYY)){
  12.         $stringDate = strftime("%d %b %Y",mktime (0,0,0,$MM,1,$YYYY));
  13.         $days = strftime("%d",mktime (0,0,0,$MM+1,0,$YYYY));
  14.         $firstDay = strftime("%w",mktime (0,0,0,$MM,1,$YYYY));
  15.         $lastDay = strftime("%w",mktime (0,0,0,$MM,$days,$YYYY));
  16.         $printDays = $days;
  17.         $preMonth = strftime("%m",mktime (0,0,0,$MM-1,1,$YYYY));
  18.         $preYear = strftime("%Y",mktime (0,0,0,$MM-1,1,$YYYY));
  19.         $nextMonth = strftime("%m",mktime (0,0,0,$MM+1,1,$YYYY));
  20.         $nextYear = strftime("%Y",mktime (0,0,0,$MM+1,1,$YYYY));
  21.         print("<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\">");
  22.         print("<tr><th valign=\"top\"><a href=\"".$_SERVER['PHP_SELF']."?NB=".$_GET["NB"]."&MM=".$preMonth."&YY=".$preYear."\">P</a></th>");
  23.         print("<th colspan=\"5\" valign=\"top\">".strftime("%b %Y",mktime (0,0,0,$MM,1,$YYYY))."</th>");
  24.         print("<th valign=\"top\"><a href=\"".$_SERVER['PHP_SELF']."?NB=".$_GET["NB"]."&MM=".$nextMonth."&YY=".$nextYear."\">N</a></th></tr>");
  25.         print("<tr style=\"font-family: Verdana; font-size:x-small\">");
  26.         print("<th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>");
  27.         $currentDays = 1;
  28.         for($a=1;$a<=5;$a++){
  29.             print("<tr align=\"left\" valign=\"top\" style=\"font-family: Verdana; font-size:x-small\">");
  30.             $diffDays = $firstDay-$lastDay;
  31.             if($firstDay>$lastDay && $currentDays ==1 && ($diffDays<>1)){
  32.                 for($x=$lastDay;$x>=0;$x--){
  33.                     $printDays = $days-$x;
  34.                     print("<td>$printDays</td>");
  35.                 }
  36.                 for($z=1;$z<$firstDay-$lastDay;$z++){
  37.                     print("<td> </td>");
  38.                 }
  39.                 for($y=$firstDay;$y<7;$y++){
  40.                     print("<td>$currentDays</td>");
  41.                     $currentDays++;
  42.                 }
  43.             } elseif($firstDay!=0 && $currentDays==1){
  44.                 for($z=1;$z<=$firstDay;$z++){
  45.                     print("<td> </td>");
  46.                 }
  47.                 for($y=$firstDay;$y<7;$y++){
  48.                     print("<td>$currentDays</td>");
  49.                     $currentDays++;
  50.                 }
  51.             } else {
  52.                 for($u=1;$u<=7 && $currentDays<=$days;$u++){
  53.                     print("<td>$currentDays</td>");
  54.                     $currentDays++;
  55.                 }
  56.             }
  57.             print("</tr>");
  58.         }
  59.         print("</table>");
  60.     }
  61. }
  62. ?>