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>   









本文转自 牛海彬 51CTO博客,原文链接:http://blog.51cto.com/newhappy/76900,如需转载请自行联系原作者
目录
相关文章
|
2月前
|
前端开发 JavaScript 机器人
用PHP实现了一个极验验证功能,如何做?具体代码如何写?
极验验证是一种防机器人的验证机制,可以通过图像识别等方式来判断用户是否为真实用户。
46 1
|
2月前
|
PHP
PHP三级联动详细代码(全)
有时在项目中常常会用到三级联动,那么怎么php怎么制作三级联动呢?话不多说,直接上代码。
24 0
|
2月前
|
XML 前端开发 JavaScript
PHP多文件压缩并分片下载文件详细介绍,附上完整代码
有时我们经常需要压缩下载多个文件,我这里主要采用在fastadmin框架中添加了一个表格自定义按钮,并为按钮绑定相应的事件来实现。
34 0
|
2月前
|
PHP
PHP的代码格式规范
PHP的代码格式规范
30 0
|
3月前
|
JSON 小程序 前端开发
微信小程序学习实录2(下拉刷新、下拉加载更多、小程序事件、PHP后端代码、刷新无数据解决方案)
微信小程序学习实录2(下拉刷新、下拉加载更多、小程序事件、PHP后端代码、刷新无数据解决方案)
86 0
|
3月前
|
JSON 关系型数据库 MySQL
php使用webSocket实现Echarts长连接自动刷新的解决方案(2):后端服务端代码返回json数据
php使用webSocket实现Echarts长连接自动刷新的解决方案(2):后端服务端代码返回json数据
49 0
|
3月前
|
API PHP
php函数API接口的一段代码,不为什么就是想放在这里
php函数API接口的一段代码,不为什么就是想放在这里
21 0
|
3月前
|
JSON 前端开发 JavaScript
Echarts实战案例代码(15):月收入年龄分段等MYSQL分类统计PHP后台数据管理接口API数据的解决方案
Echarts实战案例代码(15):月收入年龄分段等MYSQL分类统计PHP后台数据管理接口API数据的解决方案
89 0
|
3月前
|
SQL JSON API
PHP读取数据库记录转换为JSON的代码(API接口的SQL语句)
PHP读取数据库记录转换为JSON的代码(API接口的SQL语句)
29 0
相关产品
机器翻译
推荐文章
更多