Generate HTML calendar for dates


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. if (function_exists('date_default_timezone_set')) {
  6. // Set the default timezone to US/Eastern
  7. }
  8.  
  9. /**
  10.  * Will return a timestamp of the last day in a month for a specified year
  11.  *
  12.  * @param int $month
  13.  * @param int $year
  14.  *
  15.  * @return false|int
  16.  */
  17. function last_day(int $month, int $year):int {
  18. // Use mktime to create a timestamp one month into the future, but one
  19. // day less. Also make the time for almost midnight, so it can be
  20. // used as an 'end of month' boundary
  21. return mktime(23, 59, 59, $month + 1, 0, $year);
  22. }
  23.  
  24. /**
  25.  * This function will generate and return an HTML Calendar
  26.  *
  27.  * @param int $month
  28.  * @param int $year
  29.  * @param int $weekdaytostart
  30.  *
  31.  * @return string[]
  32.  */
  33. function get_html_calendar(int $month, int $year, int $weekdaytostart = 0):array {
  34. /** @var string[] $calendar */
  35. $calendar = [];
  36.  
  37. // There are things we need to know about this month such as the last day:
  38. $last = idate('d', last_day($month, $year));
  39.  
  40. // We also need to know what day of the week the first day is, and let's
  41. // let the system tell us what the name of the Month is:
  42. $firstdaystamp = mktime(0, 0, 0, $month, 1, $year);
  43. $firstwday = idate('w', $firstdaystamp);
  44. $name = date('F', $firstdaystamp);
  45.  
  46. // To easily enable our 'any day of the week start', we need to make an
  47. // array of weekday numbers, in the actual printing order we are using
  48. $weekorder = [];
  49.  
  50. for ($wo = $weekdaytostart; $wo < $weekdaytostart + 7; $wo++) {
  51. $weekorder[] = $wo % 7;
  52. }
  53.  
  54. // Now, begin our HTML table
  55. $calendar[] = sprintf('<table><tr><th colspan="7">%s %s</th></tr>', $name, $year);
  56.  
  57. // Now before we really start, print a day row:
  58. // Use the system to tell us the days of the week:
  59. $calendar[] = '<tr>';
  60.  
  61. // Loop over a full week, based upon day 1
  62. foreach ($weekorder as $w) {
  63. $dayname = date('D', mktime(0, 0, 0, $month, 1 - $firstwday + $w, $year));
  64. $calendar[] = sprintf('<th>%s</th>', $dayname);
  65. }
  66.  
  67. $calendar[] = '</tr>';
  68.  
  69. // Now we need to start some counters, and do some looping:
  70. $onday = 0;
  71. $started = FALSE;
  72.  
  73. // While we haven't surpassed the last day of the month, loop:
  74. while ($onday <= $last) {
  75. // Begin our next row of the table
  76. $calendar[] = '<tr>';
  77.  
  78. // Now loop 0 through 6, for the days of the week, but in the order
  79. // we are actually going, use mod to make this work
  80. foreach ($weekorder as $d) {
  81. // If we haven't started yet:
  82. // Does today equal the first weekday we should start on?
  83. if (!($started) && $d === $firstwday) {
  84. // Set that we have started, and increment the counter
  85. $started = TRUE;
  86. $onday++;
  87. }
  88.  
  89. // Now if the day is zero or greater than the last day make a
  90. // blank table cell.
  91. if (($onday === 0) || ($onday > $last)) {
  92. $calendar[] = '<td>&nbsp;</td>';
  93. }
  94. else {
  95. // Otherwise, echo out a day & Increment the counter
  96. $calendar[] = sprintf('<td>%s</td>', $onday);
  97. $onday++;
  98. }
  99. }
  100.  
  101. // End this table row:
  102. $calendar[] = "</tr>";
  103. }
  104.  
  105. // Now end the table:
  106. $calendar[] = '</table>';
  107.  
  108. return $calendar;
  109. }
  110.  
  111. // ========================================================
  112. // = Demo showing calendar for all months of current year =
  113. // ========================================================
  114.  
  115. // Output some formatting directives:
  116. echo '<style>table, td, th { border: 1px solid #000; }</style>';
  117.  
  118. // Create an entire year calendar for 2006 with Monday as the first day:
  119. $current_year = (int) date('Y');
  120. foreach (range(1, 12) as $month) {
  121. echo implode('', get_html_calendar($month, $current_year, 1));
  122. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.