Php simple calendar program code _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php simple calendar program code. 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 date () function provided by the PHP where the first day of the month is located, this function provides rich 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:

$ 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;
?>









$ J = 1;

While ($ j <= $ day_num)
{
Echo"


If ($ week = 6 ){
Echo "nt















For ($ I = 0; $ I <$ start_week; $ I ++){Echo" ";} ";$ Week = ($ start_week + $ J-1) % 7; N ";If ($ j! = $ Day_num)Echo "t Ntt ";Else $ end = true;}$ J ++;}While ($ week % 7! = 6){Echo" ";$ Week ++;}If (! $ End)Echo "n ";?>
Sunday Monday Tuesday Wednesday Thursday Friday Saturday
$ J

Advanced class













Echo'








































































';Foreach ($ this-> weeks as $ title){Echo' ';}Echo' ';} Private function showDays ($ year, $ month){$ FirstDay = mktime (0, 0, 0, $ month, 1, $ year );$ StarDay = date ('W', $ firstDay );$ Days = date ('t', $ firstDay ); ';For ($ I = 0; $ I <$ starDay; $ I ++ ){Echo' ';} For ($ j = 1; $ j <= $ days; $ j ++ ){$ I ++;If ($ j = date ('D ')){Echo' ';} Else {Echo' ';}If ($ I % 7 = 0 ){Echo' ';}} Echo' ';} Private function showChangeDate (){ $ Url = basename ($ _ SERVER ['php _ SELF ']); Echo' ';Echo' ';Echo' ';Echo' ';Echo' ';Echo' ';Echo' ';} 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;} }
The code is as follows:

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'





';$ This-> showChangeDate ();$ This-> showWeeks ();$ This-> showDays ($ this-> year, $ this-> month );Echo'
';
}

Private function showWeeks ()
{
Echo'
'. $ Title .'
 '. $ J .''. $ J .'
PreYearUrl ($ this-> year, $ this-> month). '">'. '<'.'PreMonthUrl ($ this-> year, $ this-> month). '">'. '<'.'NextMonthUrl ($ this-> year, $ this-> month). '">'. '> '.'NextYearUrl ($ this-> year, $ this-> month). '">'. '> '.'

Call method

The code is as follows:

$ 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 ';
?>



Calendar demo





$ Cal = new Calendar ($ params );
$ Cal-> display ();
?>




Aggregate () function, which provides rich 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 first day of the month...

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.