Published in: PHP
<?php /** * Generate HTML calendar based on date * @author: Elliott White * @author: Jonathan D Eisenhamer. * @link: http://www.quepublishing.com/articles/article.asp?p=664657&seqNum=7&rl=1 * @since: December 1, 2006. */ { // Set the default timezone to US/Eastern date_default_timezone_set( 'US/Eastern' ); } // Will return a timestamp of the last day in a month for a specified year function last_day( $month, $year ) { // Use mktime to create a timestamp one month into the future, but one // day less. Also make the time for almost midnight, so it can be // used as an 'end of month' boundary } // This function will print an HTML Calendar, given a month and year function print_calendar( $month, $year, $weekdaytostart = 0 ) { // There are 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 let's // let the system tell us what the name of the Month is: $firstwday = idate( 'w', $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 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 ) { 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> </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>'; } // ======================================================== // = Demo showing calendar for all months of current year = // ======================================================== // Output some formatting directives: echo '<style>table, td, th { border: 1px solid black; }</style>'; // Create an entire year calendar for 2006 with Monday as the first day: { echo '<br />'; } ?>
You need to login to post a comment.
