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,如需转载请自行联系原作者
目录
相关文章
|
1月前
|
IDE PHP 开发工具
PHP中的类型提示与严格模式:提高代码可维护性
随着PHP语言的发展,开发者对代码的可读性、可维护性和可靠性有了更高的要求。PHP中的类型提示(Type Hinting)和严格模式(Strict Mode)为开发者提供了更强的类型检查机制,有助于提升代码质量和减少潜在的错误,尤其是在大型项目中。
|
2月前
|
安全 程序员 PHP
PHP中的异常处理:提升代码的健壮性
【10月更文挑战第8天】在编程的世界中,错误和异常是不可避免的。它们就像路上的坑洼,可能会让我们的程序“跌倒”。但是,如果我们能够正确地处理这些异常,就可以让我们的程序更加稳健,就像我们学会了如何在坑洼的路上稳稳地行走一样。本文将介绍PHP中的异常处理机制,以及如何使用它来提升我们的代码质量。
|
14天前
|
PHP 开发者 容器
PHP命名空间深度解析:避免命名冲突与提升代码组织####
本文深入探讨了PHP中命名空间的概念、用途及最佳实践,揭示其在解决全局命名冲突、提高代码可维护性方面的重要性。通过生动实例和详尽分析,本文将帮助开发者有效利用命名空间来优化大型项目结构,确保代码的清晰与高效。 ####
18 1
|
20天前
|
供应链 安全 NoSQL
PHP 互斥锁:如何确保代码的线程安全?
在多线程和高并发环境中,确保代码段互斥执行至关重要。本文介绍了 PHP 互斥锁库 `wise-locksmith`,它提供多种锁机制(如文件锁、分布式锁等),有效解决线程安全问题,特别适用于电商平台库存管理等场景。通过 Composer 安装后,开发者可以利用该库确保在高并发下数据的一致性和安全性。
32 6
|
17天前
|
PHP 开发者 UED
探索PHP中的异常处理:提升代码的健壮性
在PHP开发中,优雅地处理错误和异常是确保应用稳定性和用户体验的关键。本文将通过深入浅出的方式,介绍如何在PHP中实现有效的异常处理机制,包括异常的基本概念、如何抛出和捕获异常,以及最佳实践。准备好让你的代码变得更加健壮和可靠吧!
17 2
|
27天前
|
PHP 开发者
PHP中的异常处理:提升代码的健壮性
【10月更文挑战第40天】在PHP编程中,异常处理是确保应用稳定性的关键。本文将引导你理解异常处理的重要性,掌握如何在PHP中捕获和处理异常,以及如何通过自定义异常类来增强代码的错误管理能力。我们将一起探索如何利用PHP的异常处理机制,打造一个更加健壮和可靠的应用程序。
|
2月前
|
设计模式 算法 数据库连接
PHP中的设计模式:提高代码的可维护性和扩展性
【10月更文挑战第13天】 本文将探讨PHP中常见的设计模式及其在实际项目中的应用。通过对比传统编程方式,我们将展示设计模式如何有效地提高代码的可维护性和扩展性。无论是单例模式确保类的单一实例,还是观察者模式实现对象间的松耦合,每一种设计模式都为开发者提供了解决特定问题的最佳实践。阅读本文后,读者将能更好地理解和应用这些设计模式,从而提升PHP编程的效率和质量。
|
2月前
|
设计模式 SQL 安全
PHP中的设计模式:单例模式的深入探索与实践在PHP开发领域,设计模式是解决常见问题的高效方案集合。它们不是具体的代码,而是一种编码和设计经验的总结。单例模式作为设计模式中的一种,确保了一个类仅有一个实例,并提供一个全局访问点。本文将深入探讨单例模式的基本概念、实现方式及其在PHP中的应用。
单例模式在PHP中的应用广泛,尤其在处理数据库连接、日志记录等场景时,能显著提高资源利用率和执行效率。本文从单例模式的定义出发,详细解释了其在PHP中的不同实现方法,并探讨了使用单例模式的优势与注意事项。通过对示例代码的分析,读者将能够理解如何在PHP项目中有效应用单例模式。
|
2月前
|
程序员 数据库连接 PHP
PHP中的异常处理:提升代码的健壮性
【9月更文挑战第30天】在编程的世界里,错误和异常是不可避免的。本文将深入探索PHP中异常处理的机制,揭示如何通过有效的异常管理来增强代码的健壮性和可靠性。我们将从基础概念出发,逐步深入到高级应用,最后通过实际代码示例来巩固理论知识。无论你是初学者还是有经验的开发者,这篇文章都将为你提供宝贵的指导和启示。
50 6
|
2月前
|
PHP 开发者 UED
PHP中的异常处理:提升代码的健壮性
【10月更文挑战第8天】在编程世界中,错误和异常是不可避免的。它们如同路上的绊脚石,让我们的程序跌倒。但如果我们学会了如何处理这些异常,就能像学会走路的孩子一样,即使跌倒也能爬起来继续前进。本文将引导你如何在PHP中捕获并处理异常,让你的代码更加健壮,能够优雅地面对各种挑战。