Posted By

MMDeveloper on 12/29/09


Tagged

calendar generator hash


Versions (?)

Month hash generator for PHP Calendar


 / Published in: PHP
 

URL: http://skyward.nefec.org/component,eventcalendar/

http://skyward.nefec.org/component,eventcalendar/

Lets say you run the function and store the returned value in a variable called $componentPointer, I then run the second block of template code to "draw" the calendar.

The "makeURL" method simply generates the "a href" URL string. To implement into your own environment, a little tailoring would be needed. A live view is at the URL above.

  1. method to generate hash:
  2. /**
  3. * This method generates a calendar hash for an entire month and returns array of data
  4. * @access private
  5. * @author Matt Ford
  6. * @param int $month integer representing month
  7. * @param int $year 4 digit year
  8. * @param boolean $countEventsOnly return number of events for each day instead of a list of events
  9. * @return array $return hash array
  10. */
  11. private function generateCalendarHash($month, $year, $countEventsOnly = false) {
  12. $firstOfMonth = mktime(0, 0, 0, $month, 1, $year);
  13. $daysInMonth = date("t", $firstOfMonth);
  14. $weekday = date("N", $firstOfMonth);
  15. $numWeeks = floor(($daysInMonth - (6 - date("w", $firstOfMonth))) / 7);
  16. $day_names = array(
  17. "Sunday",
  18. "Monday",
  19. "Tuesday",
  20. "Wednesday",
  21. "Thursday",
  22. "Friday",
  23. "Saturday"
  24. );
  25. $currentDay = 1;
  26. $weeks = array();
  27.  
  28. if ($weekday > 0 && $weekday != 7) {
  29. $week = array_pad(array(), $weekday, array("num" => null));
  30. }
  31. else {
  32. $week = array();
  33. }
  34.  
  35. while ($currentDay < $daysInMonth) {
  36. for ($i = count($week); $i < 7; $i++, $currentDay++) {
  37. if ($currentDay <= $daysInMonth) {
  38. /*
  39. This is where you'd run your queries to determine how many events fall on this day.
  40. */
  41. if ($countEventsOnly === true) {
  42. $events = array();
  43. $numEvents = 0;//$this->getNumberEvents($month, $currentDay, $year);
  44. }
  45. else {
  46. $events = array();//$this->getEvents($month, $currentDay, $year);
  47. $numEvents = 0;//count($events);
  48. }
  49.  
  50. $week[] = array("num" => $currentDay, "numEvents" => $numEvents, "events" => $events);
  51. }
  52. else {
  53. $week[] = array("num" => null);
  54. }
  55. }
  56. $weeks[] = $week;
  57. $week = array();
  58. }
  59.  
  60. $return = array (
  61. "monthName" => htmlentities(ucfirst(date("F", $firstOfMonth))),
  62. "monthInt" => $month,
  63. "year" => $year,
  64. "today" => array (
  65. "month" => date("n"),
  66. "day" => date("j"),
  67. "year" => date("Y")
  68. ),
  69. "dayNames" => $day_names,
  70. "numWeeks" => $numWeeks,
  71. "weeks" => $weeks
  72. );
  73. return $return;
  74. }
  75.  
  76.  
  77.  
  78.  
  79.  
  80. <table>
  81. <?php
  82. foreach ($componentPointer["hash"]["weeks"] as $weekNum => $week) {
  83. echo "<tr>";
  84.  
  85. foreach ($week as $dayOfWeek => $day) {
  86. $cellContents = "&nbsp;";
  87. $class = "day";
  88. $dateLinkTitle = date("l F d, Y", mktime(0, 0, 0, $componentPointer["month"], $day["num"], $componentPointer["year"]));
  89.  
  90. //$numLine = '<div class="evtCal_full_dayNumLine">' . $day["num"] . '</div>';
  91. $numLine = '<div class="evtCal_full_dayNumLine"><a href="' . $this->makeURL(array("component" => "eventcalendar", "task" => "viewDay", "month" => $componentPointer["month"], "day" => $day["num"], "year" => $componentPointer["year"])) . '" title="View Calendar Day: ' . $dateLinkTitle . '">' . $day["num"] . '</a></div>';
  92.  
  93.  
  94. switch ($day["num"]) {
  95. case null:
  96. $class .= " evtCal_full_dayNotThisMonth";
  97. break;
  98.  
  99. default:
  100. if ($day["numEvents"] > 0) {
  101. $class .= " evtCal_full_dayHasEvents";
  102. //$numLine = '<div class="evtCal_full_dayNumLine"><a href="' . $this->makeURL(array("component" => "eventcalendar", "task" => "viewDay", "month" => $componentPointer["month"], "day" => $day["num"], "year" => $componentPointer["year"])) . '">' . $day["num"] . '</a></div>';
  103. $cellContents = '<div class="evtCal_full_dayContents">' . $day["numEvents"] . " event(s)</div>";
  104. }
  105. else {
  106. $class .= " evtCal_full_dayNoEvents";
  107. }
  108. break;
  109. }
  110.  
  111. if ($componentPointer["hash"]["monthInt"] == $componentPointer["hash"]["today"]["month"] && $day["num"] == $componentPointer["hash"]["today"]["day"] && $componentPointer["hash"]["year"] == $componentPointer["hash"]["today"]["year"]) {
  112. $class .= " evtCal_full_dayToday";
  113. } else {}
  114.  
  115. echo '<td class="' . $class . '">' . $numLine . $cellContents . '</td>';
  116. }
  117. echo '<td class="evtCal_full_spacerImageCell"><img src="' . $controller_config->absolute . '/supplementalFiles/img/spacer.gif" width="1" height="80" alt="spacer" /></td>';
  118. echo "</tr>";
  119. }
  120. ?>
  121. </table>

Report this snippet  

You need to login to post a comment.