Simple and practical PHP calendar program code

Source: Internet
Author: User

The PHP calendar program features popular functions. You can pull down and switch the year from the previous year to the next year to the last year to the next year. This is too much easier to scale out, why is the title exquisite? It is because you feel that the interface is still good. Haha, let everyone laugh. Please also point out the shortcomings.

The result code is as follows:

Php calendar core code

The code is as follows: Copy code

<? Php
// Calendar
Class calendar {
// Current year
Private $ year;
// Current Month
Private $ month;
// The first day of a month is the day of the week
Private $ start_weekday;
// Number of days in the current month
Private $ days;
// Maximum and minimum number of years, maximum and minimum number of months
Private $ yearMonth = array (2080,190 0, 12, 1 );
// Constructor
Function _ construct (){
If (isset ($ _ GET ['Year']) {
$ This-> year = $ _ GET ['Year'];
        }
If (isset ($ _ GET ['month']) {
$ This-> month = $ _ GET ['month'];
        }
$ This-> pnYm ($ this-> year, $ this-> month );
$ This-> days = date ('t', mktime (0, 0, 0, $ this-> month, 1, $ this-> year ));
$ This-> start_weekday = date ('W', mktime (0, 0, 0, $ this-> month, 1, $ this-> year ));
$ This-> style ();
    }
// Output
Private function style (){
Echo '<table id = "calendar"> ';
$ This-> weeklist ();
$ This-> daylist ();
Echo '<table> ';
    }
// Date and month parameter determination
Private function ymCheck ($ year, $ month ){
If (! Is_numeric ($ year )){
$ Year = date ('Y ');
        }
If (! Is_numeric ($ month )){
$ Month = date ('M ');
        }
If ($ month <$ this-> yearMonth [3]) {
$ Month = $ this-> yearMonth [2];
$ Year-= 1;
        }
If ($ month> $ this-> yearMonth [2]) {
$ Month = $ this-> yearMonth [3];
$ Year = intval ($ year) + 1;
        }
$ Year = $ year <$ this-> yearMonth [1]? $ This-> yearMonth [1]: $ year;
$ Year = $ year> $ this-> yearMonth [0]? $ This-> yearMonth [0]: $ year;
Return array ($ year, $ month );
    }
// Last year, next year, last month, and next month
Private function pnYm ($ year, $ month ){
$ Ym = $ this-> ymCheck ($ year, $ month );
$ This-> year = $ ym [0];
$ This-> month = $ ym [1];
    }
// Weeklist weekly list
Private function weeklist (){
$ Week = array ('day', 'yi', '2', '3', '4', '5', '6 ');
Echo '<tr> ';
Foreach ($ week as $ val ){
Echo '<th>'. $ val. '</th> ';
        }
Echo '</tr> ';
    }
// Daylist day list
Private function daylist (){
// Year, month, and day navigation
Echo '<tr> ';
Echo '<td> <a title = "previous year" href = "? Year = '. ($ this-> year-1 ). '& month = '. $ this-> month. '"> </a> </td> ';
Echo '<td> <a title = "January" href = "? Year = '. $ this-> year.' & month = '. ($ this-> month-1).' "> </a> </td> ';
Echo '<td colspan = "3"> ';
Echo '<form action = "? "Method =" get "id =" form "> ';
Echo '<select name = "year" onchange = "formaction ()"> ';
For ($ I = $ this-> yearMonth [1]; $ I <= $ this-> yearMonth [0]; $ I ++ ){
If ($ I ==$ this-> year ){
Echo '<option value = "'. $ I. '" selected = "selected">'. $ I. '</option> ';
} Else {
Echo '<option value = "'. $ I. '">'. $ I. '</option> ';
             }
        }
Echo '</select> ';
Echo '<select name = "month" onchange = "formaction ()"> ';
For ($ I = $ this-> yearMonth [3]; $ I <= $ this-> yearMonth [2]; $ I ++ ){
If ($ I ==$ this-> month ){
Echo '<option value = "'. $ I. '" selected = "selected">'. $ I. 'month </option> ';
} Else {
Echo '<option value = "'. $ I. '">'. $ I. 'month </option> ';
            }
        }
Echo '</select> </form> </td> ';
Echo '<td> <a title = "Next Month" href = "? Year = '. $ this-> year.' & month = '. ($ this-> month + 1).' ">></a> </td> ';
Echo '<td> <a title = "Next year" href = "? Year = '. ($ this-> year + 1 ). '& month = '. $ this-> month. '">>></a> </td> ';
Echo '</tr> ';

Echo '<tr> ';
// Output space (the space to be blank before the first day of the current month)
For ($ I = 0; $ I <$ this-> start_weekday; $ I ++ ){
Echo '<td> & nbsp; </td> ';
       }
For ($ k = 1; $ k <= $ this-> days; $ k ++ ){
$ I ++;
If ($ k = date ('D ')){
Echo '<td>'. $ k. '</td> ';
} Else {
Echo '<td>'. $ k. '</td> ';
            }
If ($ I % 7 = 0 ){
If ($ k! = $ This-> days ){
Echo '</tr> <tr> ';
                 }
            }
        }
Echo '</tr> ';
    }
}
?>

Html + css code

The code is as follows: Copy code

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = utf-8"/>
<Title> PHP calendar program </title>
<Style>
# Calendar {width: auto; margin: 0 auto; margin-top: 100px; border: 0; border-collapse: collapse; box-shadow: 0px 0px 4px # ddd; font-size: 12px; text-align: center; font-family: ""; color: #333; border: solid 1px # c5e2ff ;}
# Calendar tr {width: auto; height: 34px; line-height: 34px ;}
# Calendar tr th {width: 44px; background: # c5e2ff ;}
# Calendar tr td {background: # fff ;}
# Calendar tr td. tdbg {background: # c5e2ff ;}
# Calendar tr td: hover {background: # FFC ;}
# Calendar tr td a {text-decoration: none; color: # f50; font-weight: 900 ;}
# Calendar select {width: auto; border: solid 1px # c5c5c5; padding: 2px 0 2px 0; background: # fff; float: left; margin-left: 5px ;}
</Style>
<Script>
Function formaction (){
Var form = document. getElementById ('form ');
Form. submit ();
}
</Script>
</Head>
<Body>
<? Php
Require 'init. Php ';
$ Calendar = new calendar ();
?>
</Body>
</Html>

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.