/ Published in: PHP

Generates HTML calendars for a given year.
Original authors: Elliott White, Jonathan D Eisenhamer
Source: http://www.quepublishing.com/articles/article.asp?p=664657&seqNum=7&rl=1
Original authors: Elliott White, Jonathan D Eisenhamer
Source: http://www.quepublishing.com/articles/article.asp?p=664657&seqNum=7&rl=1
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php declare(strict_types=1); // Set the default timezone to US/Eastern } /** * Will return a timestamp of the last day in a month for a specified year * * @param int $month * @param int $year * * @return false|int */ function last_day(int $month, int $year):int { // 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 generate and return an HTML Calendar * * @param int $month * @param int $year * @param int $weekdaytostart * * @return string[] */ /** @var string[] $calendar */ $calendar = []; // There are things we need to know about this month such as the last day: // 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: // 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 = []; for ($wo = $weekdaytostart; $wo < $weekdaytostart + 7; $wo++) { $weekorder[] = $wo % 7; } // Now, begin our HTML table // Now before we really start, print a day row: // Use the system to tell us the days of the week: $calendar[] = '<tr>'; // Loop over a full week, based upon day 1 foreach ($weekorder as $w) { } $calendar[] = '</tr>'; // 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 $calendar[] = '<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: // Does today equal the first weekday we should start on? if (!($started) && $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)) { $calendar[] = '<td> </td>'; } else { // Otherwise, echo out a day & Increment the counter $onday++; } } // End this table row: $calendar[] = "</tr>"; } // Now end the table: $calendar[] = '</table>'; return $calendar; } // ======================================================== // = Demo showing calendar for all months of current year = // ======================================================== // Output some formatting directives: echo '<style>table, td, th { border: 1px solid #000; }</style>'; // Create an entire year calendar for 2006 with Monday as the first day: }
Comments
