Build a Calendar Table


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



Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. function build_calendar($month,$year,$dateArray) {
  4.  
  5. // Create array containing abbreviations of days of week.
  6. $daysOfWeek = array('S','M','T','W','T','F','S');
  7.  
  8. // What is the first day of the month in question?
  9. $firstDayOfMonth = mktime(0,0,0,$month,1,$year);
  10.  
  11. // How many days does this month contain?
  12. $numberDays = date('t',$firstDayOfMonth);
  13.  
  14. // Retrieve some information about the first day of the
  15. // month in question.
  16. $dateComponents = getdate($firstDayOfMonth);
  17.  
  18. // What is the name of the month in question?
  19. $monthName = $dateComponents['month'];
  20.  
  21. // What is the index value (0-6) of the first day of the
  22. // month in question.
  23. $dayOfWeek = $dateComponents['wday'];
  24.  
  25. // Create the table tag opener and day headers
  26.  
  27. $calendar = "<table class='calendar'>";
  28. $calendar .= "<caption>$monthName $year</caption>";
  29. $calendar .= "<tr>";
  30.  
  31. // Create the calendar headers
  32.  
  33. foreach($daysOfWeek as $day) {
  34. $calendar .= "<th class='header'>$day</th>";
  35. }
  36.  
  37. // Create the rest of the calendar
  38.  
  39. // Initiate the day counter, starting with the 1st.
  40.  
  41. $currentDay = 1;
  42.  
  43. $calendar .= "</tr><tr>";
  44.  
  45. // The variable $dayOfWeek is used to
  46. // ensure that the calendar
  47. // display consists of exactly 7 columns.
  48.  
  49. if ($dayOfWeek > 0) {
  50. $calendar .= "<td colspan='$dayOfWeek'>&nbsp;</td>";
  51. }
  52.  
  53. $month = str_pad($month, 2, "0", STR_PAD_LEFT);
  54.  
  55. while ($currentDay <= $numberDays) {
  56.  
  57. // Seventh column (Saturday) reached. Start a new row.
  58.  
  59. if ($dayOfWeek == 7) {
  60.  
  61. $dayOfWeek = 0;
  62. $calendar .= "</tr><tr>";
  63.  
  64. }
  65.  
  66. $currentDayRel = str_pad($currentDay, 2, "0", STR_PAD_LEFT);
  67.  
  68. $date = "$year-$month-$currentDayRel";
  69.  
  70. $calendar .= "<td class='day' rel='$date'>$currentDay</td>";
  71.  
  72. // Increment counters
  73.  
  74. $currentDay++;
  75. $dayOfWeek++;
  76.  
  77. }
  78.  
  79. // Complete the row of the last week in month, if necessary
  80.  
  81. if ($dayOfWeek != 7) {
  82.  
  83. $remainingDays = 7 - $dayOfWeek;
  84. $calendar .= "<td colspan='$remainingDays'>&nbsp;</td>";
  85.  
  86. }
  87.  
  88. $calendar .= "</tr>";
  89.  
  90. $calendar .= "</table>";
  91.  
  92. return $calendar;
  93.  
  94. }
  95.  
  96. ?>
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103. Usage
  104.  
  105. Build a calendar of the current month:
  106.  
  107. <?php
  108.  
  109. $dateComponents = getdate();
  110.  
  111. $month = $dateComponents['mon'];
  112. $year = $dateComponents['year'];
  113.  
  114. echo build_calendar($month,$year,$dateArray);
  115.  
  116. ?>

URL: http://css-tricks.com/snippets/php/build-a-calendar-table/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.