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
| Sunday |
Monday |
Tuesday |
Wednesday |
Thursday |
Friday |
Saturday |
for ($i = 0; $i < $start _week; $i + +) {echo "
| ”;}
$j | "; $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
”;? >
|
Advanced one-point class
Echo '
| 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 ' |
'; foreach ($this->weeks as $title) {echo '
| . $title. | ';} 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 '
'. $j. ' | '; } else {echo '
'. $j. ' | '; if ($i% 7 = = 0) {echo '
'; }} Echo '
';} Private Function Showchangedate (){ $url = basename ($_server[' php_self '); Echo '
'; Echo '
| Preyearurl ($this->year, $this->month). ' " > '. ' << '. ' | '; Echo '
Premonthurl ($this->year, $this->month). ' " > '. ' < '. ' | '; Echo '
| '; Echo '
Nextmonthurl ($this->year, $this->month). ' " > '. ' > '. ' | '; Echo '
Nextyearurl ($this->year, $this->month). ' " > '. ' >> '. ' | '; 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;} }
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.