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
46 * @version CVS: $Id: Human.php,v 1.6 2006/11/21 17:38:15 firman Exp $
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 * @author Allan Kent <allan@lodestone.co.za>
66 * @copyright 1997-2005 Allan Kent
67 * @license http://www.opensource.org/licenses/bsd-license.php
69 * @version Release: 1.4.7
70 * @link http://pear.php.net/package/Date
71 * @since Class available since Release 1.3
75 // {{{ gregorianToHuman()
78 * Returns an associative array containing the converted date information
79 * in 'Human Calendar' format.
81 * @param int day in DD format, default current local day
82 * @param int month in MM format, default current local month
83 * @param int year in CCYY format, default to current local year
87 * @return associative array(
88 * hdom, // Human Day Of Month, starting at 1
89 * hdow, // Human Day Of Week, starting at 1
90 * hwom, // Human Week of Month, starting at 1
91 * hwoy, // Human Week of Year, starting at 1
92 * hmoy, // Human Month of Year, starting at 0
95 * If the day is New Years Day, the function will return
101 * Since 0 is a valid month number under the Human Calendar, I have left
102 * the month as -1 for New Years Day.
104 function gregorianToHuman($day=0, $month=0, $year=0)
107 * Check to see if any of the arguments are empty
108 * If they are then populate the $dateinfo array
109 * Then check to see which arguments are empty and fill
110 * those with the current date info
112 if ((empty($day) ||
(empty($month)) ||
empty($year))) {
113 $dateinfo = getdate(time());
116 $day = $dateinfo["mday"];
119 $month = $dateinfo["mon"];
122 $year = $dateinfo["year"];
125 * We need to know how many days into the year we are
127 $dateinfo = getdate(mktime(0, 0, 0, $month, $day, $year));
128 $dayofyear = $dateinfo["yday"];
130 * Human Calendar starts at 0 for months and the first day of the year
131 * is designated 00, so we need to start our day of the year at 0 for
132 * these calculations.
133 * Also, the day of the month is calculated with a modulus of 28.
134 * Because a day is 28 days, the last day of the month would have a
135 * remainder of 0 and not 28 as it should be. Decrementing $dayofyear
140 * 28 days in a month...
142 $humanMonthOfYear = floor($dayofyear / 28);
144 * If we are in the first month then the day of the month is $dayofyear
145 * else we need to find the modulus of 28.
147 if ($humanMonthOfYear == 0) {
148 $humanDayOfMonth = $dayofyear;
150 $humanDayOfMonth = ($dayofyear) %
28;
153 * Day of the week is modulus 7
155 $humanDayOfWeek = $dayofyear %
7;
157 * We can now increment $dayofyear back to it's correct value for
158 * the remainder of the calculations
162 * $humanDayOfMonth needs to be incremented now - recall that we fudged
163 * it a bit by decrementing $dayofyear earlier
164 * Same goes for $humanDayOfWeek
169 * Week of the month is day of the month divided by 7, rounded up
170 * Same for week of the year, but use $dayofyear instead $humanDayOfMonth
172 $humanWeekOfMonth = ceil($humanDayOfMonth / 7);
173 $humanWeekOfYear = ceil($dayofyear / 7);
175 * Return an associative array of the values
178 "hdom" => $humanDayOfMonth,
179 "hdow" => $humanDayOfWeek,
180 "hwom" => $humanWeekOfMonth,
181 "hwoy" => $humanWeekOfYear,
182 "hmoy" => $humanMonthOfYear );
186 // {{{ humanToGregorian()
189 * Returns unix timestamp for a given Human Calendar date
191 * @param int day in DD format
192 * @param int month in MM format
193 * @param int year in CCYY format, default to current local year
197 * @return int unix timestamp of date
199 function humanToGregorian($day, $month, $year=0)
202 * Check to see if the year has been passed through.
203 * If not get current year
206 $dateinfo = getdate(time());
207 $year = $dateinfo["year"];
210 * We need to get the day of the year that we are currently at so that
211 * we can work out the Gregorian Month and day
213 $DayOfYear = $month * 28;
216 * Human Calendar starts at 0, so we need to increment $DayOfYear
217 * to take into account the day 00
221 * the mktime() function will correctly calculate the date for out of
222 * range values, so putting $DayOfYear instead of the day of the month
225 $GregorianTimeStamp = mktime(0, 0, 0, 1, $DayOfYear, $year);
226 return $GregorianTimeStamp;
239 * c-hanging-comment-ender-p: nil