PHP Simple Calendar program code _php Tutorial

Source: Internet
Author: User
PHP provides a date () function, which provides a rich range of date processing functions. Now there are two data to be obtained, the first is the total number of days in the month, the second is the day of the week on the first of the months, and the number represents 0 (representing Sunday) to 6 (for Saturday).

The data above can be easily obtained by using the date () function

The code is as follows Copy Code

$month = $_get[' m ']?$_get[' m ']:d ate (' n ');
$year = $_get[' y ']?$_get[' y ']:d ate (' 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 one-point 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 Copy Code

Class Calendar
{
Private $year;
Private $month;
Private $weeks = Array (' Day ', ' one ', ' two ', ' three ', ' four ', ' V ', ' VI ');

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). ' " > '. ' >> '. '

Calling methods

The code is as follows Copy Code

!--? php
$params = Array ();
if (isset ($_get[') ') && 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 ();
?>



http://www.bkjia.com/PHPjc/444655.html www.bkjia.com true http://www.bkjia.com/PHPjc/444655.html techarticle PHP provides a date () function, which provides a rich range of date processing functions. Now there are two data to be obtained, the first is the total number of days in 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.