首页 > 开发 > PHP > 正文

php简单的日历程序代码

2024-05-04 21:58:56
字体:大 中 小
来源:转载
供稿:网友

PHP提供了date()函数,该函数提供了丰富的日期处理功能,现在需要获得的数据有两个,第一个是当月的总天数,第二个是该月的第一天所在星期中的第几天,数字表示0(表示星期天)到6(表示星期六).

通过date()函数可以很容易获得上面的数据,代码如下:

  1. <?php 
  2.  
  3. $month = $_GET['m']?$_GET['m']:date(‘n’); 
  4. $year = $_GET['y']?$_GET['y']:date(‘Y’); 
  5.  
  6. $start_week = date(‘w’,mktime(0,0,0,$month,1,$year)); 
  7. $day_num = date(‘t’,mktime(0,0,0,$month,1,$year)); 
  8. $end = false; 
  9. ?> 
  10. <table> 
  11. <tr> 
  12. <td>星期日</td><td>星期一</td><td>星期二</td><td>星期三</td><td>星期四</td><td>星期五</td><td>星期六</td> 
  13. </tr> 
  14. <tr> 
  15. <?php 
  16. for($i = 0; $i<$start_week; $i++) 
  17. { 
  18. echo “<td></td>”; 
  19. } 
  20.  
  21. $j=1; 
  22.  
  23. while($j<=$day_num) 
  24. { 
  25. echo “<td>$j</td>”; 
  26. $week = ($start_week+$j-1)%7; 
  27.  
  28. if($week ==6){ 
  29. echo “nt</tr>n”; 
  30. if($j != $day_num) 
  31. echo “t<tr>ntt”; 
  32. else $end = true; 
  33. } 
  34. $j++; 
  35. } 
  36. while($week%7 != 6) 
  37. { 
  38. echo “<td></td>”; 
  39. $week++; 
  40. } 
  41. if(!$end) 
  42. echo “n</tr>”; 
  43. ?> 
  44.  
  45. </table> 
  46.  
  47.  
  48. 高级一点类 
  49.  
  50.  代码如下 复制代码  
  51. <?php 
  52. class Calendar 
  53. { 
  54.     private $year; 
  55.     private $month; 
  56.     private $weeks  = array('日','一','二','三','四','五','六'); 
  57.      
  58.     function __construct($options = array()) { 
  59.         $this->year = date('Y'); 
  60.         $this->month = date('m'); 
  61.          
  62.         $vars = get_class_vars(get_class($this)); 
  63.         foreach ($options as $key=>$value) { 
  64.             if (array_key_exists($key, $vars)) { 
  65.                 $this->$key = $value; 
  66.             } 
  67.         } 
  68.     } 
  69.      
  70.     function display() 
  71.     { 
  72.         echo '<table class="calendar">'; 
  73.         $this->showChangeDate(); 
  74.         $this->showWeeks(); 
  75.         $this->showDays($this->year,$this->month); 
  76.         echo '</table>'; 
  77.     } 
  78.      
  79.     private function showWeeks() 
  80.     { 
  81.         echo '<tr>'; 
  82.         foreach($this->weeks as $title) 
  83.         { 
  84.             echo '<th>'.$title.'</th>'; 
  85.         } 
  86.         echo '</tr>'; 
  87.     } 
  88.      
  89.     private function showDays($year, $month) 
  90.     { 
  91.         $firstDay = mktime(0, 0, 0, $month, 1, $year); 
  92.         $starDay = date('w', $firstDay); 
  93.         $days = date('t', $firstDay); 
  94.  
  95.         echo '<tr>'; 
  96.         for ($i=0; $i<$starDay; $i++) { 
  97.             echo '<td>&nbsp;</td>'; 
  98.         } 
  99.          
  100.         for ($j=1; $j<=$days; $j++) { 
  101.             $i++; 
  102.             if ($j == date('d')) { 
  103.                 echo '<td class="today">'.$j.'</td>'; 
  104.             } else { 
  105.                 echo '<td>'.$j.'</td>'; 
  106.             } 
  107.             if ($i % 7 == 0) { 
  108.                 echo '</tr><tr>'; 
  109.             } 
  110.         } 
  111.          
  112.         echo '</tr>'; 
  113.     } 
  114.      
  115.     private function showChangeDate() 
  116.     { 
  117.          
  118.         $url = basename($_SERVER['PHP_SELF']); 
  119.          
  120.         echo '<tr>'; 
  121.  echo '<td><a href="?'.$this->preYearUrl($this->year,$this->month).'">'.'<<'.'</a></td>'; 
  122.  echo '<td><a href="?'.$this->preMonthUrl($this->year,$this->month).'">'.'<'.'</a></td>'; 
  123.         echo '<td colspan="3"><form>'; 
  124.          
  125.         echo '<select name="year" onchange="window.location=''.$url.'?year='+this.options[selectedIndex].value+'&month='.$this->month.''">'; 
  126.         for($ye=1970; $ye<=2038; $ye++) { 
  127.             $selected = ($ye == $this->year) ? 'selected' : ''; 
  128.             echo '<option '.$selected.' value="'.$ye.'">'.$ye.'</option>'; 
  129.         } 
  130.         echo '</select>'; 
  131.         echo '<select name="month" onchange="window.location=''.$url.'?year='.$this->year.'&month='+this.options[selectedIndex].value+''">'; 
  132.         
  133.  
  134.          
  135.         for($mo=1; $mo<=12; $mo++) { 
  136.             $selected = ($mo == $this->month) ? 'selected' : ''; 
  137.             echo '<option '.$selected.' value="'.$mo.'">'.$mo.'</option>'; 
  138.         } 
  139.         echo '</select>';         
  140.         echo '</form></td>';         
  141.  echo '<td><a href="?'.$this->nextMonthUrl($this->year,$this->month).'">'.'>'.'</a></td>'; 
  142.  echo '<td><a href="?'.$this->nextYearUrl($this->year,$this->month).'">'.'>>'.'</a></td>';         
  143.         echo '</tr>'; 
  144.     } 
  145.      
  146.     private function preYearUrl($year,$month) 
  147.     { 
  148.         $year = ($this->year <= 1970) ? 1970 : $year - 1 ; 
  149.          
  150.         return 'year='.$year.'&month='.$month; 
  151.     } 
  152.      
  153.     private function nextYearUrl($year,$month) 
  154.     { 
  155.         $year = ($year >= 2038)? 2038 : $year + 1; 
  156.          
  157.         return 'year='.$year.'&month='.$month; 
  158.     } 
  159.      
  160.     private function preMonthUrl($year,$month) 
  161.     { 
  162.         if ($month == 1) { 
  163.             $month = 12; 
  164.             $year = ($year <= 1970) ? 1970 : $year - 1 ; 
  165.         } else { 
  166.             $month--; 
  167.         }         
  168.          
  169.         return 'year='.$year.'&month='.$month; 
  170.     } 
  171.      
  172.     private function nextMonthUrl($year,$month) 
  173.     { 
  174.         if ($month == 12) { 
  175.             $month = 1; 
  176.             $year = ($year >= 2038) ? 2038 : $year + 1; 
  177.         }else{ 
  178.             $month++; 
  179.         } 
  180.         return 'year='.$year.'&month='.$month; 
  181.     } 
  182.      
  183. } 
  184.  
  185.  
  186. 调用方法 
  187.  
  188.  代码如下 复制代码  
  189. <?php 
  190. $params = array(); 
  191. if (isset($_GET['year']) && isset($_GET['month'])) { 
  192.     $params = array( 
  193.         'year' => $_GET['year'], 
  194.         'month' => $_GET['month'], 
  195.     ); 
  196. } 
  197. $params['url']  = 'demo.php'; 
  198. require_once 'calendar.class.php'; 
  199. ?> 
  200.  
  201. <html> 
  202.     <head> 
  203.         <title>日历demo</title> 
  204.         <meta http-equiv="Content-Type" content="text/html" charset="UTF-8" /> 
  205.         <style type="text/css"> 
  206.             table.calendar { 
  207.                 border: 1px solid #050; 
  208.             } 
  209.             .calendar th, .calendar td { 
  210.                 width:30px; 
  211.                 text-align:center; 
  212.             }             
  213.             .calendar th { 
  214.                 background-color:#050; 
  215.                 color:#fff; 
  216.             } 
  217.             .today{ 
  218.   color:#fff; 
  219.   background-color:#050;   
  220. //开源代码Vevb.com               
  221.             } 
  222.         </style> 
  223.     </head> 
  224.     <body> 
  225.         <div style="align:center"> 
  226.             <?php 
  227.                 $cal = new Calendar($params); 
  228.                 $cal->display(); 
  229.             ?>     
  230.         </div> 
  231.     </body> 
  232. </html> 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表