A good PHP calendar class sharing

Source: Internet
Author: User
This article mainly introduces a relatively good PHP calendar class sharing. This article provides the class code and usage examples for implementation, for more information about how to process periods and times, you must compile the calendar program. However, when writing a calendar, most people think that the calendar only serves to display the current date on the page. In fact, calendar plays a more important role in our development. For example, if we develop a "notepad", we need to set the date through the calendar. in other systems, we need to schedule tasks by date and calendar. Not many date and time functions are involved in this example. They are described earlier. They are mainly used to compile a calendar class to consolidate the object-oriented syntax knowledge discussed earlier, and time function applications, the main thing is to improve the thinking logic and programming capabilities of beginners. Declare the Calendar class calendar in the file Calendar. class. php. the code is as follows:

<? Php // file: calendar. class. php Calendar class original file error_reporting (0); class Calendar {private $ year; private $ month; private $ start_weekday; // The Day of the month corresponds to the day of the week, as the start private $ days of the current month's start traversal date; // The total number of days of the current month // The constructor, used to initialize some date attributes function _ construct () {// If you have not set the number of parts, use the year $ this-> year = isset ($ _ GET ["year"]) of the current system time? $ _ GET ["year"]: date ("Y"); // if the number of months is not set, use the month of the current system Time $ this-> month = isset ($ _ GET ["month"])? $ _ GET ["month"]: date ("m"); // use date () the w parameter of the function gets the number of weeks corresponding to the first day of the current month. $ this-> start_weekday = date ("w", mktime (0, 0, 0, $ this-> month, 1, $ this-> year); // use date () by year and month () the t parameter of the function is used to obtain the number of days in the current month $ this-> days = date ("t", mktime (, 0, $ this-> month, 1, $ this-> year);} // The Magic method is used to print the entire calendar function _ toString () {$ out. ='
 
 
  
  
'; $ Out. = $ this-> chageDate (); // call the internal private method to set the date $ out. = $ this-> weeksList (); // call the internal private method to print the weekly list $ out. = $ this-> daysList (); // call the internal private method to print the daily list $ out. ='
 
 
'; Return $ out; // returns all strings required for the entire calendar input} // private method called internally, used to output the weekly list private function weeksList () {$ week = array ('day', 'yi', '2', '3', '4', '5', '6'); $ out. =''; For ($ I = 0; $ I '; // The first row is in the tableOutput weekly list $ out. =''; Return $ out; // returns the weekly list string} // private method called internally, used to output the weekly list private function daysList () {$ out. =''; // Output space (to be blank before the first day of the current month) for ($ j = 0; $ j <$ this-> start_weekday; $ j ++) $ out. =' '; // Cyclically traverse all the dates of the current month. if it is the current date, set a dark background for it ($ k = 1; $ k <= $ this-> days; $ k ++) {$ j ++; if ($ k = date ('D') {$ out. =''. $ K .'';} Else {$ out. =''. $ K .'';} If ($ j % 7 = 0) // Change $ out. =' for every 7 dates output''; // End of the output row and start of the next row} // after the end date is traversed, the while ($ j % 7! = 0) {$ out. =' '; $ J ++;} $ out. =''; Return $ out; // returns the date list of the current month} // private method called internally, used to process the data required for the previous year of the current year. private function prevYear ($ year, $ month) {$ year = $ year-1; // The last year is the current year minus 1 if ($ year <1970) // The minimum value of the year is 1970 $ year = 1970; return "year = {$ year} & month = {$ month}"; // return the final year and month setting parameters} // private method called internally, private function prevMonth ($ year, $ month) {if ($ month = 1) {$ year = $ year-1; // the last year is the current year minus 1 if ($ year <1970) // The minimum value of the year is 1970 $ year = 1970; $ month = 12; // if the current year is January, last month is the last month of the previous year} else {$ month --; // the last month is the current month minus 1} return "year = {$ year} & month = {$ month }"; // return the final year and month setting parameters} // private method called internally, used to process data of the next year of the current year. private function nextYear ($ year, $ month) {$ year = $ year + 1; // The next year is the current year plus 1 if ($ year> 2038) // The maximum value of the year is 2038 $ year = 2038; return "year = {$ year} & month = {$ month}"; // return the final year and month setting parameters} // private method called internally, private function nextMonth ($ year, $ month) {if ($ month = 12) {$ year ++; if ($ year> 2038) // The maximum value of the year is 2038 $ year = 2038; $ month = 1; // if it is January, last month is the last month of the previous year} else {$ month ++; // the last month is the current month minus 1} return "year = {$ year} & month = {$ month }"; // return the final year and month setting parameters} // private method called internally, used for user operations to adjust the year and month setting private function chageDate ($ url = "index. php ") {$ out. =''; $ Out. ='PrevYear ($ this-> year, $ this-> month). '">'. '<'.''; $ Out. ='PrevMonth ($ this-> year, $ this-> month). '">'. '<'.''; $ Out. =''; $ Out. = ''; $ out. =''; $ Out. ='NextYear ($ this-> year, $ this-> month). '">'. '> '.''; $ Out. ='NextMonth ($ this-> year, $ this-> month). '">'. '> '.''; $ Out. =''; Return $ out; // return date form}?>

In this example, a calendar program is split by function (Week list, date list, set date, and settings for the previous year, next year, last month, and next month) and encapsulated in a calendar class. With the calendar class, we also need to write another main program to load and output the calendar. in the main program, we also need to set the calendar output style first. the code is as follows:

            Ncong PHP calendar example                <? Php require 'Calendar. class. php'; echo new calendar;?>

Running result. the current system date is displayed by default. You can click ">" to set the next year, but set the maximum year to 2038. You can also click <to set the last year, but set the minimum year to 1970. You can also click the "<" and ">" buttons to set the previous and next months. if the current month is December, the next month is January of the following year. if the current month is January, set the last month to the month of the previous year. If you need to quickly locate the specified year and month, you can also set it in the drop-down list.

Related Article

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.