2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
7 * Class to convert date strings between Gregorian and Human calendar formats
9 * The Human Calendar format has been proposed by Scott Flansburg and can be
10 * explained as follows:
11 * The year is made up of 13 months
12 * Each month has 28 days
13 * Counting of months starts from 0 (zero) so the months will run from 0 to 12
14 * New Years day (00) is a monthless day
15 * Note: Leap Years are not yet accounted for in the Human Calendar system
17 * PHP versions 4 and 5
21 * Copyright (c) 1997-2006 Allan Kent
22 * All rights reserved.
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted under the terms of the BSD License.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
40 * @category Date and Time
42 * @author Allan Kent <allan@lodestone.co.za>
43 * @copyright 1997-2006 Allan Kent
44 * @license http://www.opensource.org/licenses/bsd-license.php
47 * @link http://pear.php.net/package/Date
48 * @since File available since Release 1.3
52 // {{{ Class: Date_Human
55 * Class to convert date strings between Gregorian and Human calendar formats
57 * The Human Calendar format has been proposed by Scott Flansburg and can be
58 * explained as follows:
59 * The year is made up of 13 months
60 * Each month has 28 days
61 * Counting of months starts from 0 (zero) so the months will run from 0 to 12
62 * New Years day (00) is a monthless day
63 * Note: Leap Years are not yet accounted for in the Human Calendar system
65 * @category Date and Time
67 * @author Allan Kent <allan@lodestone.co.za>
68 * @copyright 1997-2005 Allan Kent
69 * @license http://www.opensource.org/licenses/bsd-license.php
71 * @version Release: 1.5.0a4
72 * @link http://pear.php.net/package/Date
73 * @since Class available since Release 1.3
77 // {{{ gregorianToHuman()
80 * Returns an associative array containing the converted date information
81 * in 'Human Calendar' format.
83 * If the day is New Years Day, the function will return
89 * Since 0 is a valid month number under the Human Calendar, I have left
90 * the month as -1 for New Years Day.
92 * @param int $day in DD format, default current local day
93 * @param int $month in MM format, default current local month
94 * @param int $year in CCYY format, default to current local year
96 * @return associative array(
97 * hdom, // Human Day Of Month, starting at 1
98 * hdow, // Human Day Of Week, starting at 1
99 * hwom, // Human Week of Month, starting at 1
100 * hwoy, // Human Week of Year, starting at 1
101 * hmoy, // Human Month of Year, starting at 0
106 public function gregorianToHuman($day = 0, $month = 0, $year = 0)
109 * Check to see if any of the arguments are empty
110 * If they are then populate the $dateinfo array
111 * Then check to see which arguments are empty and fill
112 * those with the current date info
114 if ((empty($day) || (empty($month)) || empty($year))) {
115 $dateinfo = getdate(time());
118 $day = $dateinfo["mday"];
121 $month = $dateinfo["mon"];
124 $year = $dateinfo["year"];
127 * We need to know how many days into the year we are
129 $dateinfo = getdate(mktime(0, 0, 0, $month, $day, $year));
130 $dayofyear = $dateinfo["yday"];
132 * Human Calendar starts at 0 for months and the first day of the year
133 * is designated 00, so we need to start our day of the year at 0 for
134 * these calculations.
135 * Also, the day of the month is calculated with a modulus of 28.
136 * Because a day is 28 days, the last day of the month would have a
137 * remainder of 0 and not 28 as it should be. Decrementing $dayofyear
142 * 28 days in a month...
144 $humanMonthOfYear = floor($dayofyear / 28);
146 * If we are in the first month then the day of the month is $dayofyear
147 * else we need to find the modulus of 28.
149 if ($humanMonthOfYear == 0) {
150 $humanDayOfMonth = $dayofyear;
152 $humanDayOfMonth = ($dayofyear) % 28;
155 * Day of the week is modulus 7
157 $humanDayOfWeek = $dayofyear % 7;
159 * We can now increment $dayofyear back to it's correct value for
160 * the remainder of the calculations
164 * $humanDayOfMonth needs to be incremented now - recall that we fudged
165 * it a bit by decrementing $dayofyear earlier
166 * Same goes for $humanDayOfWeek
171 * Week of the month is day of the month divided by 7, rounded up
172 * Same for week of the year, but use $dayofyear instead $humanDayOfMonth
174 $humanWeekOfMonth = ceil($humanDayOfMonth / 7);
175 $humanWeekOfYear = ceil($dayofyear / 7);
177 * Return an associative array of the values
179 return array("hdom" => $humanDayOfMonth,
180 "hdow" => $humanDayOfWeek,
181 "hwom" => $humanWeekOfMonth,
182 "hwoy" => $humanWeekOfYear,
183 "hmoy" => $humanMonthOfYear);
187 // {{{ humanToGregorian()
190 * Returns unix timestamp for a given Human Calendar date
192 * @param int $day in DD format
193 * @param int $month in MM format
194 * @param int $year in CCYY format, default to current local year
196 * @return int unix timestamp of date
200 public function humanToGregorian($day, $month, $year = 0)
203 * Check to see if the year has been passed through.
204 * If not get current year
207 $dateinfo = getdate(time());
208 $year = $dateinfo["year"];
211 * We need to get the day of the year that we are currently at so that
212 * we can work out the Gregorian Month and day
214 $DayOfYear = $month * 28;
217 * Human Calendar starts at 0, so we need to increment $DayOfYear
218 * to take into account the day 00
222 * the mktime() function will correctly calculate the date for out of
223 * range values, so putting $DayOfYear instead of the day of the month
226 $GregorianTimeStamp = mktime(0, 0, 0, 1, $DayOfYear, $year);
227 return $GregorianTimeStamp;
240 * c-hanging-comment-ender-p: nil