PHP实现的简单日历类

这篇文章主要介绍了PHP实现的简单日历类,可实现简单的日期显示功能,并高亮标注当前日期,具有一定的实用价值,需要的朋友可以参考下

本文实例讲述了PHP实现的简单日历类。分享给大家供大家参考。

具体实现代码如下:

  1. date_default_timezone_set("etc/gmt-8");
  2. header("Content-type: text/html; charset=utf-8");
  3. class calendar{
  4. var $t = array();
  5. var $datesofmonth = array('1'=>'31','2'=>'28','3'=>'31','4'=>'30','5'=>'31','6'=>'30','7'=>'31','8'=>'31','9'=>'30','10'=>'31','11'=>'30','12'=>'31');
  6. var $y,$m,$d;
  7. function set($time){
  8. $this->t = getdate($time);
  9. $this->y = $this->t['year'];
  10. $this->m = $this->t['mon'];
  11. $this->d = date('d',$time);
  12. }
  13. function isrun(){
  14. return ($this->y%400==0 || ($this->y%4==0 && $this->y%100==0)) ? 1 : 0;
  15. }
  16. function first(){
  17. $time = mktime(0,0,0,$this->m,1,$this->y);
  18. $time = getdate($time);
  19. return $time['wday'];
  20. }
  21. function html(){
  22. $isrun = $this->isrun();
  23. $this->datesofmonth[2] = $isrun==1 ? 29: 28;
  24. $html .= "<table >n";
  25. $html .= "<tr><th><a href=''>上一月</a></th><th colspan='5'>{$this->y}年 {$this->m}月</th><th><a href=''>下一月</a></th><tr>n";
  26. $html .= "<tr><td>星期天</td><td>星期一</td><td>星期二</td>jb51.net<td>星期三</td><td>星期四</td><td>星期五</td><td>星期六</td></tr>n";
  27. $html .= "<tr>n";
  28. $first = $this->first();
  29. for($i=0; $i<$first; $i++){
  30. $html .= "<td></td>";
  31. }
  32. $count = $this->datesofmonth[$this->m]+$first;
  33. for ($i=1; $i<= $this->datesofmonth[$this->m]; $i++){
  34. $style = $i==$this->d ? ' ' : '' ;
  35. $html .= "<td align='center'{$style}>$i</td>";
  36. if (($i==7%$first || ($i+$first)%7==0) && $i<$count){
  37. $html .= "</tr>n<tr>";
  38. }
  39. }
  40. $count = 7-$count%7;
  41. if ($count<7){
  42. for ($i=0; $i<$count; $i++){
  43. $html .= "<td></td>";
  44. }
  45. }
  46. $html .= "</tr>n";
  47. $html .= "</table>n";
  48. return $html;
  49. }
  50. }
  51. $calendar = new calendar();
  52. $calendar->set(time());
  53. echo $calendar->html();

希望本文所述对大家的php程序设计有所帮助。