03c07-1.php

来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 92 行

PHP
92
字号
<?phpob_start();require '03c02-1.php';ob_end_clean();// This function will print an HTML Calendar, given a month and yearfunction print_calendar($month, $year, $weekdaytostart = 0) {	// There things we need to know about this month such as the last day:	$last = idate('d', last_day($month, $year));		// We also need to know what day of the week the first day is, and lets	//  let the system tell us what the name of the Month is:	$firstdaystamp = mktime(0, 0, 0, $month, 1, $year);	$firstwday = idate('w', $firstdaystamp);	$name = date('F', $firstdaystamp);		// To easily enable our 'any day of the week start', we need to make an	//  array of weekday numbers, in the actual printing order we are using	$weekorder = array();	for ($wo = $weekdaytostart; $wo < $weekdaytostart + 7; $wo++) {		$weekorder[] = $wo % 7;	}		// Now, begin our HTML table	echo "<table><tr><th colspan=\"7\">{$name} {$year}</th></tr>\n";	// Now before we really start, print a day row:	// Use the system to tell us the days of the week:	echo '<tr>';	// Loop over a full week, based upon day 1	foreach ($weekorder as $w) {		$dayname = date('D', 			mktime(0, 0, 0, $month, 1 - $firstwday + $w, $year));		echo "<th>{$dayname}</th>";	}	echo "</tr>\n";		// Now we need to start some counters, and do some looping:	$onday = 0;	$started = false;		// While we haven't surpassed the last day of the month, loop:	while ($onday <= $last) {		// Begin our next row of the table		echo '<tr>';		// Now loop 0 through 6, for the days of the week, but in the order		//  we are actually going, use mod to make this work		foreach ($weekorder as $d) {			// If we haven't started yet:			if (!($started)) {				// Does today equal the first weekday we should start on?				if ($d == $firstwday) {					// Set that we have started, and increment the counter					$started = true;					$onday++;				}			}						// Now if the day is zero or greater than the last day make a			//  blank table cell.			if (($onday == 0) || ($onday > $last)) {				echo '<td>&nbsp;</td>';			} else {				// Otherwise, echo out a day & Increment the counter				echo "<td>{$onday}</td>";				$onday++;			}		}				// End this table row:		echo "</tr>\n";	}		// Now end the table:	echo '</table>';}// Output some formatting directives:echo '<style>table, td, th { border: 1px solid black; }</style>';// Now make a couple sample tables:// November 2005, with the default (Sunday) as the first day:print_calendar(11, 2005);echo '<br />';// Create an entire year calendar for 2006 with Monday as the first day:foreach(range(1, 12) as $m) {	print_calendar($m, 2006, 1);	echo '<br />';}?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?