A relatively good PHP calendar class sharing _php instance

Source: Internet
Author: User

When it comes to dealing with times and times, it's important to introduce the calendar program. But when it comes to writing calendars, most people think that the calendar works just to show the current date on the page, but the calendar has a more important role in our development. For example, when we develop a "notepad" we need to set the date by calendar, and some systems need to schedule tasks by date, calendars, and so on. This example involves a number of date and time functions are not many, are the previous introduction of the content, mainly through a calendar class of writing, to consolidate the previous introduction of the object-oriented grammar knowledge, as well as the application of time function, the most important thing is to improve the beginner's thinking logic and program design capabilities. To declare calendar class calendars in file calendar.class.php, the code looks like this:

<?php  //     file:calendar.class.php   Calendar class original file    error_
Reporting (0);
      class calendar{        private $year;
        private $month;         private $start _weekday;            //The first day of the month corresponds to the week, as the beginning of the month traversal date of the start          Private $days;                / /Current Month total days           //construction method, used to initially make some date attributes          function __construct () {             //If the user does not set the number of copies, use the year              $this of the current system time- >year = Isset ($_get["year")? $_get["YeAr "]: Date (" Y ");             //If the user does not set the number of months, use the month of the current system time               $this->month = isset ($_get["month"])?
$_get["Month"]: Date ("M");             //through a specific year and month, using date () The W parameter of the function gets the first day of the month corresponds to the week              $this->start_
Weekday = Date ("W", Mktime (0,0,0, $this->month,1, $this->year));
            //uses the T parameter of the date () function to get the number of days of the month through a specific year and month              $this->days = date ("T", Mktime (0,0,0,$
this->month,1, $this->year)); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}           // The Magic method is used to print the entire calendar         function __tostring () {&NBSP;&NBSP;&NBSP;&NBSP;&NBsp;        $out. = ' <table align= ' center ' > ';              $out. = $this->chagedate ();        //Calling internal private methods for users to set their own date               $out. = $this->weekslist ();       //Call internal private method print week list               $out. = $this->dayslist ();         //Call internal private method print Day list          
    $out. = ' </table> ';               return $out;   
      //Return all the strings required for the entire calendar        &nbsp.}           //private method of internal invocation, for output week list         private function Weekslist () {            $
Week = Array (' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six ');
             $out. = ' <tr> ';
            for ($i = 0; $i <count ($week); $i + +)                   $ out. = ' <th class= ' fontb ' > '. $week [$i]. ' </th> ';        //First line with table <th> output Week list     
         $out. = ' </tr> ';             return $out;          //Return week list string        &nbsp}            //private method of internal invocation, for output week list         private function Dayslist () {           
  $out. = ' <tr> ';             //output space (to be vacated before the first day of January)              for ($j = 0; $j < $this->start_weekday; $j + +)                     $out. = '
<td> </td> ';               //loops through all the dates of the month, if it's the current date, Set the dark background             for ($k = 1; $k <= $this->days ;
$k + +) {                 $j + +;                   if ($k = = Date (' d ')) {                      $out. = ' <td class= ' FONTB '
> '. $k. ' </td> ';                 }else {                     
   $out. = ' <td> ' $k. ' </td> ';                 }                    if ($j%7 = 0)                    // 7 dates per output, change one line                        $out. = ' </tr><tr> ';       // The output line ends and the next line starts             }                //after the date, the following space is padded with             while ($j%7!== 0) {                                      $out. = '
<td> </td> ';
                 $j + +;             }        
        $out. = ' </tr> ';             return $out;                      // Back to the date list of the month &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}           //private methods for internal calls to handle data required for the previous year in the current year         private function Prevyear ($year, $month) {              $year = $year -1;          //Previous year is the current annual minus 1               if ($year < 1970)          //year set minimum value is 1970       
         $year = 1970;               return "year={$year}&month={$ Month} ";       //return final year and month setting parameters         &NBSP}           //private methods for internal calls to handle data needed for the current month in January          private functionPrevmonth ($year, $month) {              if ($ month = = 1) {                $ Year = $year -1;         //Last year is the current annual minus 1                    if ($year < 1970)           /year setting minimum value is 1970          
            $year = 1970;                  $month = 12;           //If it is January, last month in the previous year              }else {                      $month--;             // Last January is the current monthly minus 1                           return "year={$year}&month={$month}";        //Returns the final year and month setting parameters         }            //a private method of internal invocation that handles data for the next year of the current year          private function Nextyear ($year, $month) {              $year = $year +1;         //Next year is the current year plus 1                if ($year > 2038)           /year Set maximum value is 2038                       $year = 2038;               return "year={$year}&month={$ Month} ";       //return final year and month setting parameters         &NBSP}           //private methods for internal calls to handle data required for the next January of the current month          private function Nextmonth ($year, $month) {               if ($month = =) {         
         $year ++;                            if ($year > 2038)         /year set maximum value is 2038                       $year = 2038;                  $month = 1;           //If it is January, last month in the previous year              }else {                      $month ++;              //Last January is the current monthly minus 1          &NBSP;&NBSP;&NBSP;&NBSP}             return "year={ $year}&month={$month} ";       //return final year and month setting parameters     
&NBSP;&NBSP;&NBSP;&NBSP}           //private methods for internal calls, for user actions to adjust settings for year and month         private function chagedate ($url = "index.php") {&NBsp;            $out. = ' <tr> ';              $out. = ' <td><a href= '. $url '. $this->prevyear ($this->year, $this->month). " > '. ' << '. '
</a></td> ';              $out. = ' <td><a href= '. $url '. $this->prevmonth ($this->year, $this->month). " > '. ' << '. '
</a></td> ';
               $out. = ' <td colspan= ' 3 ">";
             $out. = ' <form> ';              $out. = ' <select name= ' year "onchange="
Window.location=\ '. $url.              ' year=\ ' +this.options[selectedindex]. Value+\ ' &month= '. $this->Month. '
\ ' > ';
            for ($sy =1970; $sy <=2038; $sy + +) {                  $selected = ($sy = = $this->year)?
"Selected": "";                  $out. = ' < Option '. $selected. ' value= '. $sy. ' " > '. $sy. '
</option> ';             }       
       $out. = ' </select> ';              $out. = ' <select name= ' month ' onchange=
"Window.location=\". $url.              ' year= '. $this->year. '
&month=\ ' +this.options[selectedindex].value ' > ';             for ($sm = 1;$SM <=12; $SM + +) {                  $selected 1 = ($sm = = $this->month)?
"Selected": "";                  $out. = ' <
Option '. $selected 1. ' value= '. $sm. ' > ' $sm. ' </option> ';             }       
       $out. = ' </select> ';
             $out. = ' </form> ';
             $out. = ' </td> ';                $out. = ' <td><a href= '. $ URL. '? '. $this->nextyear ($this->year, $this->month). " > '. ' >> '. '
</a></td> ';             $out. = ' <td><a href= '. $url '. $this->nextmonth ($this->year, $this->month). " > '. ' >> '. '
</a></td> ';
             $out. = ' </tr> ';             return $out;                //Return date form          }     }?>

This example splits a calendar program by Function (week list section, date list section, set Date section, and Settings section for previous year, next year, previous month, and next January) and is encapsulated in a calendar class. With the Calendar class, we also need to write a main program to load and output the calendar, in the main program also need to set the style of the calendar output, the code is as follows:

 

The results of the run are shown in the figure, and the current system date is displayed by default. You can set the next year by clicking the >> button, but the maximum year is set to 2038 years. You can also set the previous year by clicking the << button, but the minimum year is set to 1970 years. You can also set the previous and next month by clicking the < > button, and if the month is December, the next month is the January of the following year, and if the month is January, the previous month is the December of the previous one. If you need to quickly navigate to the specified year and month, you can also set it by using the Drop-down list.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.