PHP实现的简单日历代码

简介: //calendar.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                                
          }                                                                
   else                                                                  
   {                                                                     
     $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">         
                                                                           
<head>                                                                     
                                                                           
<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>                                                                      

相关文章
|
2月前
|
移动开发 人工智能 前端开发
【PHP】PHP生成全年日历
【PHP】PHP生成全年日历
100 5
|
8月前
|
开发框架 JavaScript Java
推荐一个日历转换开源工具库,支持C#、Java、PHP等主流的语言
推荐一个日历转换开源工具库,支持C#、Java、PHP等主流的语言
64 0
|
算法 JavaScript 前端开发
PHP广播日历是什么意思?底层原理是什么?
PHP广播日历是什么意思?底层原理是什么?
|
前端开发 JavaScript PHP
PHP 自制日历
最近的一个项目中,需要将数据用日历方式显示,网上有很多的JS插件,后面为了自己能有更大的控制权,决定自己制作一个日历显示。如下图所示:
PHP 自制日历
|
PHP 网络架构 索引
|
PHP
PHP生成日历数组,减少分页
2016年11月14日 20:45:41 星期一 情景, 每个用户设置定投, app的屏幕比较小, 觉得常规的线性分页下拉不是很方便 吧列表改为日历格式的会更方便执行, 每年12个月, 总过12个页面 像这种的: 代码: 1 $year = '2016'; 2 $list ...
980 0
|
前端开发 JavaScript PHP
使用PHP和AJAX制作日历
<p style="margin-top:10px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; color:rgb(102,102,102); font-family:'Open Sans',HelveticaNeue-Light,'Helvetica Neue Light','Helvetica Neue',Helve
1572 0
|
5天前
|
数据库
基于PHP+MYSQL开发制作的趣味测试网站源码
基于PHP+MYSQL开发制作的趣味测试网站源码。可在后台提前设置好缘分, 自己手动在数据库里修改数据,数据库里有就会优先查询数据库的信息, 没设置的话第一次查询缘分都是非常好的 95-99,第二次查就比较差 , 所以如果要你女朋友查询你的名字觉得很好 那就得是她第一反应是查和你的缘分, 如果查的是别人,那不好意思,第二个可能是你。
20 3
|
26天前
|
NoSQL 关系型数据库 MySQL
linux服务器重启php,nginx,redis,mysql命令
linux服务器重启php,nginx,redis,mysql命令
32 1