PHP创建日历,一个使用方便的类文件

//创建一个日期类
class  Calendar
{
     var  $startDay =0;
     var  $startMonth =1;
     var  $dayNames = array ( "Sunday" ,
        "Monday" ,
        "Tuesday" ,
         "Wednesday" ,
         "Thursday" ,
        "Friday" ,
         "Saturday" );
     var  $monthNames = array ( "January" ,
       "February" ,
     "March" ,
     "April" ,
     "May" ,
     "June" ,
          "July" ,
     "August" ,
     "September" ,
     "October" ,
     "November" ,
     "December" );
     var  $daysInMonth  array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
     function  setDayNames( $names )
     {
         $this ->dayNames= $names ;
     }
     function  getDayNames()
     {
         return  $this ->dayNames;
     }
     function  setMonthNames( $names )
     {
         $this ->monthNames= $names ;
     }
     function  getMonthNames()
     {
         return  $this ->monthNames;
     }
     function  setStartDay( $day )
     {
         $this ->startDay= $day ;
     }
     //获取一个开始日期的函数
     function  getStartDay()
     {
         return  $this ->startDay;
     }
     function  setStartMonth( $month )
     {
         $this ->startMonth= $month ;
     }
     function  getStartMonth()
     {
         return  $this ->startMonth;
     }
     function  getCalendarLink( $month , $year )
     {
         return  "" ;
     }
     //声明一个获取日期链接的函数
     function  getDateLink( $day , $month , $year )
     {
         $link = "counter.php?day=$day&month=$month&year=$year&fromcounter=1" ;
         return  "$link" ;
     }
     //设置日期链接
     function  getCurrentMonthView()
     {
         $d = getdate (time());
         return  $this ->getMonthView( $d [ "mon" ], $d [ "year" ]);
     }
     //获取当前年份
     function  getCurrentYearView()
     {
         $d = getdate (time());
         return  $this ->getYearView( $d [ "year" ]);
     }
     //声明一个获取指定年,月的函数
     function  getMonthView( $month , $year )
     {
         return  $this ->getMonthHTML( $month , $year );
     }
     //声明一个获取指定年的月份信息的函数
     function  getYearView( $year )
     {
         return  $this ->getYearHTML( $year );
     }
     //获取每月的天数
     function  getDaysInMonth( $month , $year )
     {
         if ( $month <1 ||  $month >12)
         {
             return  0;
         }
         $d = $this ->daysInMonth[ $month -1];
         if ( $month ==2)
         {
             if ( $year %4==0)
             {
                 if ( $year %100==0)
                 {
                     if ( $year %400==0)
                     {
                         $d =29;
                     }
                 }
                 else
                 {
                     $d =29;
                 }
             }
         }
         return  $d ;
     }
     //声明一个获取月份的getMonthHTML函数
     function  getMonthHTML( $m , $y , $showYear =1)
     {
         $s = "" ;
         $a = $this ->adjustDate( $m , $y );
         $month = $a [0];
         $year = $a [1];
         $daysInMonth = $this ->getDaysInMonth( $month , $year );
         $date = getdate ( mktime (12,0,0, $month ,1, $year ));
         $first = $date [ 'wday' ];
         $monthName = $this ->monthNames[ $month -1];
         $prev = $this ->adjustDate( $month -1, $year );
         $next = $this ->adjustDate( $month +1, $year );
         if ( $showYear ==1)
         {
             $prevMonth = $this ->getCalendarLink( $prev [0], $prev [1]);
             $nextMonth = $this ->getCalendarLink( $next [0], $next [1]);
         }
         else
         {
             $prevMonth = "" ;
             $nextMonth = "" ;
         }
         $header = $monthName .(( $showYear >0) ?  " . $year  "" );
         $s .= " ;
         $s .= "" ;
         $s .= "" .(( $prevMonth == "" ) ?  " "  "<<" ). "" ;
         $s .= "$header" ;
         $s .= "" .(( $nextMonth == "" ) ?  " "  ">>" ). "" ;
         $s .= "" ;
         $s .= "" ;
         $s .= "" . $this ->dayNames[( $this ->startDay)%7]. "" ;
         $s .= "" . $this ->dayNames[( $this ->startDay+1)%7]. "" ;
         $s .= "" . $this ->dayNames[( $this ->startDay+2)%7]. "" ;
         $s .= "" . $this ->dayNames[( $this ->startDay+3)%7]. "" ;
         $s .= "" . $this ->dayNames[( $this ->startDay+4)%7]. "" ;
         $s .= "" . $this ->dayNames[( $this ->startDay+5)%7]. "" ;
         $s .= "" . $this ->dayNames[( $this ->startDay+6)%7]. "" ;
         $s .= "" ;
         $d = $this ->startDay+1- $first ;
         while ( $d >1)
         {
             $d -=7;
         }
         $today = getdate (time());
         while ( $d <= $daysInMonth )
         {
             $s .= "" ;
             for ( $i =0; $i <7; $i ++)
             {
                 $class =( $year == $today [ 'year' ] &&  $month == $today [ 'mon' ] &&  $d == $today [ 'mday' ]) ?  "calenderToday"  "calendar" ;
                 $s .= "" ;
                 if ( $d >0 &&  $d <= $daysInMonth )
                 {
                     $link = $this ->getDateLink( $d , $month , $year );
                     $mtime = mktime ();
                     $dnow = date ( "d" , $mtime );
                     $mnow = date ( "m" , $mtime );
                     if ( $dnow == $d  &&  $mnow == $month )
                     {
                         $s .=(( $link == "" ) ?  $d  "$d" );
                     }
                     else
                     {
                         $s .=(( $link == "" ) ?  $d  "$d" );
                     }
                 }
                 else
                 {
                     $s .= " " ;
                 }
                 $s .= "" ;
                 $d ++;
             }
             $s .= "" ;
         }
         $s .= "" ;
         return  $s ;
     }
 
     //声明一个获取年份的HTML
     function  getYearHTML( $year )
     {
         $s = "" ;
         $prev = $this ->getCalendarLink(0, $year -1);
         $next = $this ->getCalendarLink(0, $year +1);
         $s .= "" ;
         $s .= "" ;
         $s .= "" .(( $prev == "" ) ?  " "  "<<" ). "" ;
         $s .= "" .(( $this ->startMonth>1) ?  $year . "-" .( $year +1) :  $year ). "" ;
         $s .= "" .(( $next == "" ) ?  " "  ">>" ). "" ;
         $s .= "" ;
         $s .= "" ;
         $s .= "" . $this ->getMonthHTML(0+ $this ->startMonth, $year ,0). "" ;
         $s  .=  ""  $this ->getMonthHTML(1 +  $this ->startMonth,  $year , 0) . "" ;
         $s  .=  ""  $this ->getMonthHTML(2 +  $this ->startMonth,  $year , 0) . "" ;
         $s  .=  "" ;
         $s  .=  "" ;
         $s  .=  ""  $this ->getMonthHTML(3 +  $this ->startMonth,  $year , 0) . "" ;
         $s  .=  ""  $this ->getMonthHTML(4 +  $this ->startMonth,  $year , 0) . "" ;
         $s  .=  ""  $this ->getMonthHTML(5 +  $this ->startMonth,  $year , 0) . "" ;
         $s  .=  "" ;
         $s  .=  "" ;
         $s  .=  ""  $this ->getMonthHTML(6 +  $this ->startMonth,  $year , 0) . "" ;
         $s  .=  ""  $this ->getMonthHTML(7 +  $this ->startMonth,  $year , 0) . "" ;
         $s  .=  ""  $this ->getMonthHTML(8 +  $this ->startMonth,  $year , 0) . "" ;
         $s  .=  "" ;
         $s  .=  "" ;
         $s  .=  ""  $this ->getMonthHTML(9 +  $this ->startMonth,  $year , 0) . "" ;
         $s  .=  ""  $this ->getMonthHTML(10 +  $this ->startMonth,  $year , 0) . "" ;
         $s  .=  ""  $this ->getMonthHTML(11 +  $this ->startMonth,  $year , 0) . "" ;
         $s  .=  "" ;
         $s  .=  "" ;
         return  $s ;
     }
     //声明一个日期调整函数
     function  adjustDate( $month , $year )
     {
         $a = array ();
         $a [0]= $month ;
         $a [1]= $year ;
         while ( $a [0]>12)
         {
             $a [0]-=12;
             $a [1]++;
         }
         while ( $a [0]<=0)
         {
             $a [0]+=12;
             $a [1]--;
         }
         return  $a ;
     }
 
}
class  MyCalendar  extends  Calendar
{
     function  getCalendarLink( $month , $year )
     {
         $s = getenv ( 'SCRIPT_NAME' ); //相当于$_SERVER['SCRIPT_NAME']
         return  "$s?month=$month&year=$year" ;
     }
}
?>
 

转载于:https://www.cnblogs.com/tcode/p/5915295.html

你可能感兴趣的:(php)