Calendar Function


SUBMITTED BY: scripts4you

DATE: Oct. 22, 2015, 3:15 p.m.

FORMAT: Text only

SIZE: 2.4 kB

HITS: 1326

  1. <?php
  2. function build_calendar($month,$year,$dateArray) {
  3. // Create array containing abbreviations of days of week.
  4. $daysOfWeek = array('S','M','T','W','T','F','S');
  5. // What is the first day of the month in question?
  6. $firstDayOfMonth = mktime(0,0,0,$month,1,$year);
  7. // How many days does this month contain?
  8. $numberDays = date('t',$firstDayOfMonth);
  9. // Retrieve some information about the first day of the
  10. // month in question.
  11. $dateComponents = getdate($firstDayOfMonth);
  12. // What is the name of the month in question?
  13. $monthName = $dateComponents['month'];
  14. // What is the index value (0-6) of the first day of the
  15. // month in question.
  16. $dayOfWeek = $dateComponents['wday'];
  17. // Create the table tag opener and day headers
  18. $calendar = "<table class='calendar'>";
  19. $calendar .= "<caption>$monthName $year</caption>";
  20. $calendar .= "<tr>";
  21. // Create the calendar headers
  22. foreach($daysOfWeek as $day) {
  23. $calendar .= "<th class='header'>$day</th>";
  24. }
  25. // Create the rest of the calendar
  26. // Initiate the day counter, starting with the 1st.
  27. $currentDay = 1;
  28. $calendar .= "</tr><tr>";
  29. // The variable $dayOfWeek is used to
  30. // ensure that the calendar
  31. // display consists of exactly 7 columns.
  32. if ($dayOfWeek > 0) {
  33. $calendar .= "<td colspan='$dayOfWeek'>&nbsp;</td>";
  34. }
  35. $month = str_pad($month, 2, "0", STR_PAD_LEFT);
  36. while ($currentDay <= $numberDays) {
  37. // Seventh column (Saturday) reached. Start a new row.
  38. if ($dayOfWeek == 7) {
  39. $dayOfWeek = 0;
  40. $calendar .= "</tr><tr>";
  41. }
  42. $currentDayRel = str_pad($currentDay, 2, "0", STR_PAD_LEFT);
  43. $date = "$year-$month-$currentDayRel";
  44. $calendar .= "<td class='day' rel='$date'>$currentDay</td>";
  45. // Increment counters
  46. $currentDay++;
  47. $dayOfWeek++;
  48. }
  49. // Complete the row of the last week in month, if necessary
  50. if ($dayOfWeek != 7) {
  51. $remainingDays = 7 - $dayOfWeek;
  52. $calendar .= "<td colspan='$remainingDays'>&nbsp;</td>";
  53. }
  54. $calendar .= "</tr>";
  55. $calendar .= "</table>";
  56. return $calendar;
  57. }
  58. ?>

comments powered by Disqus