天天看點

PHP實作的簡單月曆代碼

//calendar.php

<?                                                                          

 /*******************************                                           

  *   用來判斷是否閏年的函數    *                                           

  *   可以根據更複雜的算法改進  *                                           

  *******************************/                                          

  function leap_year($year)                                                 

    {                                                                  

      if($year% 4 == 0) // basic rule                                       

            {                                                               

             return true; // is leap year                                   

            }                                                               

   else                                                                   

     {                                                                    

      return false;                                                       

     }                                                                    

    }                                                                       

  *   對一些變量進行指派操作    *                                           

  *   特别注意對二月份的指派    *                                           

  function setup()                                                          

   {                                                                        

   global $mon_num;                                                       

   $mon_num=array(31,30,31,30,31,30,31,31,30,31,30,31);                   

     global $mon_name;                                                      

   $mon_name=array("一","二","三","四",                                   

                  "五","六","七","八",                                    

                  "九","十","十一","十二");                               

   if (leap_year($firstday[year])) // basic rule                          

          {                                                                 

            $mon_num[1]=29; // is leap year                                 

          }                                                                 

   {                                                                      

     $mon_num[1]=28;                                                      

   }                                                                      

   }                                                                        

  *   顯示表格中的一格          *                                           

  *   顯示的内容和顔色可變      *                                           

        function showline($content,$show_color)                             

        {                                                                   

         $begin_mark = "<TD WIDTH=60 HEIGHT=25>";                           

  $begin_mark =$begin_mark."<FONT COLOR=$show_color>";                    

  $end_mark = "</FONT></TD>";                                             

         echo $begin_mark.$content.$end_mark ;                              

        }                                                                   

?>                                                                          

<!--月曆程式的正式開始-->                                                   

<html>                                                                      

<head>                                                                      

<title>                                                                     

社群月曆                                                                    

</title>                                                                    

<meta http-equiv=content-type content="text/html; charset=gb2312">          

<body>                                                                      

   //獲得目前的日期                                                         

   $firstday  = getdate(mktime(0,0,0,date("m"),1,date("Y")));               

   setup();                                                                 

        //顯示表格的名稱                                                    

 echo "<CENTER>";                                                         

 echo "<TABLE BORDER=2  CELLSPACING=4>";                                  

 echo "<TH COLSPAN=7 HEIGHT=50>";                                         

 echo "<FONT COLOR=red SIZE=3 >";                                         

 echo "$firstday[year]年 &nbsp".$mon_name[$firstday[mon]-1]."月&nbsp月曆";

 echo "</FONT>";                                                          

 echo "</TH>";                                                            

        //表頭                                                              

 $weekDay[0] = "日";                                                      

 $weekDay[1] = "一";                                                      

 $weekDay[2] = "二";                                                      

 $weekDay[3] = "三";                                                      

 $weekDay[4] = "四";                                                      

 $weekDay[5] = "五";                                                      

 $weekDay[6] = "六";                                                      

 echo "<TR ALIGN=\"center\" VALIGN=\"center\">";                          

 //顯示表格的第一行                                                       

 for ($dayNum = 0; $dayNum < 7; ++$dayNum) {                              

    showline($weekDay[$dayNum],"red");                                    

 }                                                                        

 echo"</TR>";                                                             

   $toweek=$firstday[wday];//本月的第一天是星期幾                           

   $lastday=$mon_num[$firstday[mon]-1];//本月的最後一天是星期幾             

   $day_count = 1;//目前應該顯示的天數                                      

 $up_to_firstday = 1;//是否顯示到本月的第一天                             

 for ($row = 0; $row <= ($lastday+$toweek-1)/7; ++$row)//本月有幾個星期   

 { echo "<TR ALIGN=center VALIGN=center>";                          

  for ($col=1; $col<=7; ++$col)                                    

  {                                                                

          //在第一天前面顯示的都是"空"                             

   if (($up_to_firstday <= $toweek) ||($day_count>$lastday))

   {                                                        

    echo "<TD>&nbsp</TD>";                           

    $up_to_firstday++;                               

   }                                                        

   else                                                     

          {                                                        

                //顯示本月中的某一天                                        

               showline($day_count,"blue");                                 

    $day_count++;                                    

                 }                                                        

  }                                                                

  echo "</TR>";                                                    

 echo "</TABLE>";                                                         

 echo "</CENTER>";                                                        

</body>                                                                     

</html>   

本文轉自 牛海彬 51CTO部落格,原文連結:http://blog.51cto.com/newhappy/76900,如需轉載請自行聯系原作者