Source for file holiday-defs.php

Documentation is available at holiday-defs.php

  1. <?php
  2. /* ******************************************************************** */
  3. /* CATALYST PHP Source Code */
  4. /* -------------------------------------------------------------------- */
  5. /* This program is free software; you can redistribute it and/or modify */
  6. /* it under the terms of the GNU General Public License as published by */
  7. /* the Free Software Foundation; either version 2 of the License, or */
  8. /* (at your option) any later version. */
  9. /* */
  10. /* This program is distributed in the hope that it will be useful, */
  11. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  12. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  13. /* GNU General Public License for more details. */
  14. /* */
  15. /* You should have received a copy of the GNU General Public License */
  16. /* along with this program; if not, write to: */
  17. /* The Free Software Foundation, Inc., 59 Temple Place, Suite 330, */
  18. /* Boston, MA 02111-1307 USA */
  19. /* -------------------------------------------------------------------- */
  20. /* */
  21. /* Filename: holiday-defs.php */
  22. /* Author: Paul Waite */
  23. /* Description: Utility datetime functions to handle public holidays. */
  24. /* */
  25. /* ******************************************************************** */
  26. /** @package datetime */("datetime-defs.php");
  27.  
  28. // ----------------------------------------------------------------------
  29. /**
  30. * A class to handle a list of public holidays. It allows you to add
  31. * holidays in various ways, and also to query whether a particular
  32. * date-time falls on a holiday.
  33. */
  34. class holidays {
  35. // Array of holiday timestamps..
  36. var $hols = array();
  37. // Last matched holiday description..
  38. var $matched_holiday_desc = "";
  39. // ....................................................................
  40. /** Constructor. Optionally pass in an associative array with the key
  41. * of a unique holiday description, and value as the timestamp of the
  42. * holiday itself.
  43. * @param array $hols Associative array 'Holiday desc' => timestamp
  44. */
  45. function holidays($hols="") {
  46. if (is_array($hols)) {
  47. $this->hols = $hols;
  48. }
  49. } // holidays
  50.  
  51. // ....................................................................
  52. /** Add a holiday timestamp to the array. This is the internal method
  53. * for adding raw holiday timestamps.
  54. * @param string $holdesc Unique descriptive identifier for the holiday
  55. * @param string $holts The holiday timestamp to add
  56. */
  57. function add_holiday_ts($holdesc, $holts) {
  58. $this->hols[$holdesc] = $holts;
  59. } // add_holiday_ts
  60. // ....................................................................
  61. /** Add a holiday date as a date-string in the usual kinds of form
  62. * such as '25-12', or '25 December 2007' or '25/12/2007' etc. This
  63. * is a friendly method for adding general holiday dates.
  64. * @param string $holdesc Unique descriptive identifier for the holiday
  65. * @param string $holdate The holiday date as a string eg '25/12/2006'
  66. */
  67. function add_holiday($holdesc, $holdate) {
  68. $holts = strtotime($holdate);
  69. if ($holts != -1) {
  70. $this->add_holiday_ts($holdesc, $holts);
  71. }
  72. } // add_holiday
  73. // ....................................................................
  74. /** Add a holiday which is calculated as being the Nth occurence
  75. * of a given dayname, in a given month. This method is a common one
  76. * for designating National holidays, eg. 'Queen's Birthday' as
  77. * being the first Monday in June.
  78. * @param string $holdesc Unique descriptive identifier for the holiday
  79. * @param string $monthname Name of month eg. 'Dec' or 'December'
  80. * @param string $dayabbrev Dayname abbreviation eg. 'Mon', 'Tue'..
  81. * @param integer $n Occurence in month eg. 1, 2, 3..
  82. * @param integer $year Optional year (defaults to current year)
  83. */
  84. function add_holiday_relative($holdesc, $monthname, $dayabbrev, $n, $year="") {
  85. $holts = get_nth_day_of_month($monthname, $dayabbrev, $n, $year);
  86. if ($holts != -1) {
  87. $this->add_holiday($holdesc, $holts);
  88. }
  89. } // add_holiday_relative
  90. // ....................................................................
  91. /**
  92. * Check if the given timestamp is the date of a public holiday. We
  93. * return true if it is, else false. Note: if the timestamp falls on
  94. * a holiday, then the holiday description is left in the class var
  95. * 'matched_holiday_desc' on exit. If the passed-in timestamp is not
  96. * given, then the method assume 'now' as the timestamp.
  97. * @param integer $timestamp Unix timestamp of the date to check.
  98. * @return boolean True if the timestamp is on a public holiday
  99. */
  100. function is_holiday($timestamp="") {
  101. if ($timestamp == "") {
  102. $timestamp = time();
  103. }
  104. $ishol = false;
  105. $this->matched_holiday_desc = "";
  106. foreach ($this->hols as $holdesc => $hol) {
  107. if (strstr($hol, "-")) {
  108. $hol_ts = strtotime($hol);
  109. }
  110. else {
  111. $bits = explode(" ", $hol);
  112. $hol_ts = get_nth_day_of_month($bits[2], $bits[0], $bits[1]);
  113. }
  114. if (is_same_day($timestamp, $hol_ts)) {
  115. $ishol = true;
  116. $this->matched_holiday_desc = $holdesc;
  117. break;
  118. }
  119. } // foreach
  120. return $ishol;
  121. } // is_holiday
  122.  
  123.  
  124.  
  125. } // class holidays
  126. // ----------------------------------------------------------------------
  127.  
  128. /**
  129. * Return true if the two timestamps are on the same day of the calendar.
  130. * @return boolean True if timestamps lie on the very same day.
  131. */
  132. function is_same_day($ts1, $ts2) {
  133. $dt1 = getdate($ts1);
  134. $dt2 = getdate($ts2);
  135. return ($dt1["mday"] == $dt2["mday"]
  136. && $dt1["mon"] == $dt2["mon"]
  137. && $dt1["year"] == $dt2["year"]
  138. );
  139. } // same_day
  140. // ----------------------------------------------------------------------
  141.  
  142. /**
  143. * Return the timestamp of the date for the Nth dayname in the given
  144. * month. Returns -1 if error encountered.
  145. * @param string $monthname Month the day should be in
  146. * @param string $dayabbrev The 3-char abbrev: Eg. "Mon", "Tue", ..
  147. * @param integer $n If you want the 4th day, then this would be 4
  148. * @param integer $year Optional year, otherwise current year
  149. * @return integer Timestamp of date, or -1 if error.
  150. */
  151. function get_nth_day_of_month($monthname, $dayabbrev, $n, $year="") {
  152. global $daywk;
  153.  
  154. if ($year == "") {
  155. $now = getdate();
  156. $year = $now["year"];
  157. }
  158. // Get day index, or default to 1 (Monday)..
  159. if (isset($daywk[$dayabbrev])) {
  160. $dayno = $daywk[$dayabbrev];
  161. }
  162. else {
  163. $dayno = 1;
  164. }
  165. // Get daynum (0=Sun, 1=Mon..)
  166. $start_month_ts = strtotime("1st $monthname $year");
  167. $start_month_dayno = strftime("%w", $start_month_ts);
  168. $inc = ($dayno - $start_month_dayno + 7) % 7;
  169. $n -= 1;
  170. $required_date_ts = strtotime("1st $monthname $year + $inc days + $n weeks");
  171. return $required_date_ts;
  172. } // get_nth_day_of_month
  173. // ----------------------------------------------------------------------
  174.  
  175. /** Get the days of Easter for the given year (defaults to current year)
  176. * as an associative array of values, with the following arrangement:
  177. * returned_array['Good Friday'] = timestamp of Good Friday
  178. * returned_array['Easter Saturday'] = timestamp of Easter Saturday
  179. * returned_array['Easter Sunday'] = timestamp of Easter Sunday
  180. * returned_array['Easter Monday'] = timestamp of Easter Monday
  181. */
  182. function get_easter_holidays($year="") {
  183. global $monthnames;
  184. if ($year == "") {
  185. $year = yearNow();
  186. }
  187. $c = (int) ($year / 100);
  188. $n = $year - 19 * ((int) ($year / 19));
  189. $k = (int) (($c - 17) / 25);
  190. $i = $c - ((int) ($c / 4)) - ((int) (($c - $k) / 3)) + (19 * $n) + 15;
  191. $i = $i - 30 * ((int) ($i / 30));
  192. $i = $i - ((int) (i / 28)) * (1 - ((int) ($i / 28)) * ((int) (29 / ($i + 1))) * ((int) ((21 - $n) / 11)) );
  193. $j = $year + ((int) ($year / 4)) + $i + 2 - $c + ((int) ($c / 4));
  194. $j = $j - 7 * ((int) ($j / 7));
  195. $l = $i - $j;
  196. $mth = 3 + ((int) (($l + 40) / 44));
  197. $monthname = $monthnames[$mth];
  198. $day = $l + 28 - 31 * ((int) ($mth / 4));
  199. $easter_sunday_ts = strtotime("$day $monthname $year");
  200.  
  201. $easter = array();
  202. $easter["Good Friday"] = $easter_sunday_ts - (2 * 86400);
  203. $easter["Easter Saturday"] = $easter_sunday_ts - (1 * 86400);
  204. $easter["Easter Sunday"] = $easter_sunday_ts;
  205. $easter["Easter Monday"] = $easter_sunday_ts + (1 * 86400);
  206. // Return array of timestamps..
  207. return $easter;
  208. } // get_easter_holidays
  209. // ----------------------------------------------------------------------
  210.  
  211. ?>

Documentation generated by phpDocumentor 1.3.0RC3