Php simple calendar program code

Source: Internet
Author: User

PHP provides the date () function, which provides a variety of date processing functions. There are two types of data to be obtained: the first is the total number of days of the month, and the second is the number of days of the week in which the first day of the month is located. The number indicates 0 (indicating Sunday) to 6 (Saturday ).

The date () function can easily obtain the above data.

The Code is as follows: Copy code

<? Php

$ Month = $ _ GET ['M']? $ _ GET ['M']: date ('n ');
$ Year = $ _ GET ['y']? $ _ GET ['y']: date ('y ');

$ Start_week = date ('w', mktime (0, 0, 0, $ month, 1, $ year ));
$ Day_num = date ('T', mktime (0, 0, 0, $ month, 1, $ year ));
$ End = false;
?>
<Table>
<Tr>
<Td> Sunday </td> <td> Monday </td> <td> Tuesday </td> <td> Wednesday </td> <td> Thursday </td> <td> Friday </td> <td> Saturday </td>
</Tr>
<Tr>
<? Php
For ($ I = 0; $ I <$ start_week; $ I ++)
{
Echo "<td> </td> ";
}

$ J = 1;

While ($ j <= $ day_num)
{
Echo "<td> $ j </td> ";
$ Week = ($ start_week + $ J-1) % 7;

If ($ week = 6 ){
Echo "nt </tr> n ";
If ($ j! = $ Day_num)
Echo "t <tr> ntt ";
Else $ end = true;
}
$ J ++;
}
While ($ week % 7! = 6)
{
Echo "<td> </td> ";
$ Week ++;
}
If (! $ End)
Echo "n </tr> ";
?>

</Table>

Advanced Class

The Code is as follows: Copy code

<? Php
Class Calendar
{
Private $ year;
Private $ month;
Private $ weeks = array ('day', 'yi', '2', '3', '4', '5', '6 ');

Function _ construct ($ options = array ()){
$ This-> year = date ('y ');
$ This-> month = date ('M ');

$ Vars = get_class_vars (get_class ($ this ));
Foreach ($ options as $ key => $ value ){
If (array_key_exists ($ key, $ vars )){
$ This-> $ key = $ value;
}
}
}

Function display ()
{
Echo '<table class = "calendar"> ';
$ This-> showChangeDate ();
$ This-> showWeeks ();
$ This-> showDays ($ this-> year, $ this-> month );
Echo '</table> ';
}

Private function showWeeks ()
{
Echo '<tr> ';
Foreach ($ this-> weeks as $ title)
{
Echo '<th>'. $ title. '</th> ';
}
Echo '</tr> ';
}

Private function showDays ($ year, $ month)
{
$ FirstDay = mktime (0, 0, 0, $ month, 1, $ year );
$ StarDay = date ('w', $ firstDay );
$ Days = date ('T', $ firstDay );

Echo '<tr> ';
For ($ I = 0; $ I <$ starDay; $ I ++ ){
Echo '<td> & nbsp; </td> ';
}

For ($ j = 1; $ j <= $ days; $ j ++ ){
$ I ++;
If ($ j = date ('D ')){
Echo '<td class = "today">'. $ j. '</td> ';
} Else {
Echo '<td>'. $ j. '</td> ';
}
If ($ I % 7 = 0 ){
Echo '</tr> <tr> ';
}
}

Echo '</tr> ';
}

Private function showChangeDate ()
{

$ Url = basename ($ _ SERVER ['php _ SELF ']);

Echo '<tr> ';
Echo '<td> <a href = "? '. $ This-> preYearUrl ($ this-> year, $ this-> month ). '"> '. '<'. '</a> </td> ';
Echo '<td> <a href = "? '. $ This-> preMonthUrl ($ this-> year, $ this-> month ). '"> '. '<'. '</a> </td> ';
Echo '<td colspan = "3"> <form> ';

Echo '<select name = "year" onchange = "window. location =''. $ url .'? Year = '+ this. options [selectedIndex]. value +' & month = '. $ this-> month. ''"> ';
For ($ ye = 1970; $ ye <= 2038; $ ye ++ ){
$ Selected = ($ ye = $ this-> year )? 'Selected ':'';
Echo '<option'. $ selected. 'value = "'. $ ye.'"> '. $ ye.' </option> ';
}
Echo '</select> ';
Echo '<select name = "month" onchange = "window. location =''. $ url .'? Year = '. $ this-> year.' & month = '+ this. options [selectedIndex]. value + ''"> ';


For ($ mo = 1; $ mo <= 12; $ mo ++ ){
$ Selected = ($ mo = $ this-> month )? 'Selected ':'';
Echo '<option'. $ selected. 'value = "'. $ mo.'"> '. $ mo.' </option> ';
}
Echo '</select> ';
Echo '</form> </td> ';
Echo '<td> <a href = "? '. $ This-> nextMonthUrl ($ this-> year, $ this-> month ). '"> '. '> '. '</a> </td> ';
Echo '<td> <a href = "? '. $ This-> nextYearUrl ($ this-> year, $ this-> month ). '"> '. '> '. '</a> </td> ';
Echo '</tr> ';
}

Private function preYearUrl ($ year, $ month)
{
$ Year = ($ this-> year <= 1970 )? 1970: $ year-1;

Return 'year = '. $ year.' & month = '. $ month;
}

Private function nextYearUrl ($ year, $ month)
{
$ Year = ($ year> = 2038 )? 2038: $ year + 1;

Return 'year = '. $ year.' & month = '. $ month;
}

Private function preMonthUrl ($ year, $ month)
{
If ($ month = 1 ){
$ Month = 12;
$ Year = ($ year <= 1970 )? 1970: $ year-1;
} Else {
$ Month --;
}

Return 'year = '. $ year.' & month = '. $ month;
}

Private function nextMonthUrl ($ year, $ month)
{
If ($ month = 12 ){
$ Month = 1;
$ Year = ($ year> = 2038 )? 2038: $ year + 1;
} Else {
$ Month ++;
}
Return 'year = '. $ year.' & month = '. $ month;
}

}

Call Method

The Code is as follows: Copy code

<? Php
$ Params = array ();
If (isset ($ _ GET ['Year']) & isset ($ _ GET ['month']) {
$ Params = array (
'Year' => $ _ GET ['Year'],
'Month' => $ _ GET ['month'],
);
}
$ Params ['url'] = 'demo. php ';
Require_once 'calendar. class. php ';
?>

<Html>
<Head>
<Title> calendar demo </title>
<Meta http-equiv = "Content-Type" content = "text/html" charset = "UTF-8"/>
<Style type = "text/css">
Table. calendar {
Border: 1px solid #050;
}
. Calendar th,. calendar td {
Width: 30px;
Text-align: center;
}
. Calendar th {
Background-color: #050;
Color: # fff;
}
. Today {
Color: # fff;
Background-color: #050;
}
</Style>
</Head>
<Body>
<Div style = "align: center">
<? Php
$ Cal = new Calendar ($ params );
$ Cal-> display ();
?>
</Div>
</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.