PageRenderTime 28ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/www/devel/upgrade/upms_server/lib/pear/Date/Human.php

https://bitbucket.org/valmy/openx
PHP | 188 lines | 53 code | 4 blank | 131 comment | 10 complexity | 2a5d22075203a9cf6cf5343022717766 MD5 | raw file
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Allan Kent <allan@lodestone.co.za> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id$
  20. //
  21. /**
  22. * Class to convert date strings between Gregorian and Human calendar formats.
  23. * The Human Calendar format has been proposed by Scott Flansburg and can be
  24. * explained as follows:
  25. * The year is made up of 13 months
  26. * Each month has 28 days
  27. * Counting of months starts from 0 (zero) so the months will run from 0 to 12
  28. * New Years day (00) is a monthless day
  29. * Note: Leap Years are not yet accounted for in the Human Calendar system
  30. *
  31. * @since PHP 4.0.4
  32. * @author Allan Kent <allan@lodestone.co.za>
  33. */
  34. class Date_Human
  35. {
  36. /**
  37. * Returns an associative array containing the converted date information
  38. * in 'Human Calendar' format.
  39. *
  40. * @param int day in DD format, default current local day
  41. * @param int month in MM format, default current local month
  42. * @param int year in CCYY format, default to current local year
  43. *
  44. * @access public
  45. *
  46. * @return associative array(
  47. * hdom, // Human Day Of Month, starting at 1
  48. * hdow, // Human Day Of Week, starting at 1
  49. * hwom, // Human Week of Month, starting at 1
  50. * hwoy, // Human Week of Year, starting at 1
  51. * hmoy, // Human Month of Year, starting at 0
  52. * )
  53. *
  54. * If the day is New Years Day, the function will return
  55. * "hdom" => 0
  56. * "hdow" => 0
  57. * "hwom" => 0
  58. * "hwoy" => 0
  59. * "hmoy" => -1
  60. * Since 0 is a valid month number under the Human Calendar, I have left
  61. * the month as -1 for New Years Day.
  62. */
  63. function gregorianToHuman($day=0, $month=0, $year=0)
  64. {
  65. /**
  66. * Check to see if any of the arguments are empty
  67. * If they are then populate the $dateinfo array
  68. * Then check to see which arguments are empty and fill
  69. * those with the current date info
  70. */
  71. if ((empty($day) || (empty($month)) || empty($year))) {
  72. $dateinfo = getdate(time());
  73. }
  74. if (empty($day)) {
  75. $day = $dateinfo["mday"];
  76. }
  77. if (empty($month)) {
  78. $month = $dateinfo["mon"];
  79. }
  80. if (empty($year)) {
  81. $year = $dateinfo["year"];
  82. }
  83. /**
  84. * We need to know how many days into the year we are
  85. */
  86. $dateinfo = getdate(mktime(0, 0, 0, $month, $day, $year));
  87. $dayofyear = $dateinfo["yday"];
  88. /**
  89. * Human Calendar starts at 0 for months and the first day of the year
  90. * is designated 00, so we need to start our day of the year at 0 for
  91. * these calculations.
  92. * Also, the day of the month is calculated with a modulus of 28.
  93. * Because a day is 28 days, the last day of the month would have a
  94. * remainder of 0 and not 28 as it should be. Decrementing $dayofyear
  95. * gets around this.
  96. */
  97. $dayofyear--;
  98. /**
  99. * 28 days in a month...
  100. */
  101. $humanMonthOfYear = floor($dayofyear / 28);
  102. /**
  103. * If we are in the first month then the day of the month is $dayofyear
  104. * else we need to find the modulus of 28.
  105. */
  106. if ($humanMonthOfYear == 0) {
  107. $humanDayOfMonth = $dayofyear;
  108. } else {
  109. $humanDayOfMonth = ($dayofyear) % 28;
  110. }
  111. /**
  112. * Day of the week is modulus 7
  113. */
  114. $humanDayOfWeek = $dayofyear % 7;
  115. /**
  116. * We can now increment $dayofyear back to it's correct value for
  117. * the remainder of the calculations
  118. */
  119. $dayofyear++;
  120. /**
  121. * $humanDayOfMonth needs to be incremented now - recall that we fudged
  122. * it a bit by decrementing $dayofyear earlier
  123. * Same goes for $humanDayOfWeek
  124. */
  125. $humanDayOfMonth++;
  126. $humanDayOfWeek++;
  127. /**
  128. * Week of the month is day of the month divided by 7, rounded up
  129. * Same for week of the year, but use $dayofyear instead $humanDayOfMonth
  130. */
  131. $humanWeekOfMonth = ceil($humanDayOfMonth / 7);
  132. $humanWeekOfYear = ceil($dayofyear / 7);
  133. /**
  134. * Return an associative array of the values
  135. */
  136. return array(
  137. "hdom" => $humanDayOfMonth,
  138. "hdow" => $humanDayOfWeek,
  139. "hwom" => $humanWeekOfMonth,
  140. "hwoy" => $humanWeekOfYear,
  141. "hmoy" => $humanMonthOfYear );
  142. }
  143. /**
  144. * Returns unix timestamp for a given Human Calendar date
  145. *
  146. * @param int day in DD format
  147. * @param int month in MM format
  148. * @param int year in CCYY format, default to current local year
  149. *
  150. * @access public
  151. *
  152. * @return int unix timestamp of date
  153. */
  154. function HumanToGregorian($day, $month, $year=0)
  155. {
  156. /**
  157. * Check to see if the year has been passed through.
  158. * If not get current year
  159. */
  160. if (empty($year)) {
  161. $dateinfo = getdate(time());
  162. $year = $dateinfo["year"];
  163. }
  164. /**
  165. * We need to get the day of the year that we are currently at so that
  166. * we can work out the Gregorian Month and day
  167. */
  168. $DayOfYear = $month * 28;
  169. $DayOfYear += $day;
  170. /**
  171. * Human Calendar starts at 0, so we need to increment $DayOfYear
  172. * to take into account the day 00
  173. */
  174. $DayOfYear++;
  175. /**
  176. * the mktime() function will correctly calculate the date for out of
  177. * range values, so putting $DayOfYear instead of the day of the month
  178. * will work fine.
  179. */
  180. $GregorianTimeStamp = mktime(0, 0, 0, 1, $DayOfYear, $year);
  181. return $GregorianTimeStamp;
  182. }
  183. }
  184. ?>