]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - extlib/Date/Human.php
[ROUTES] Allow accept-header specification during router creation
[quix0rs-gnu-social.git] / extlib / Date / Human.php
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
3
4 // {{{ Header
5
6 /**
7  * Class to convert date strings between Gregorian and Human calendar formats
8  *
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
16  *
17  * PHP versions 4 and 5
18  *
19  * LICENSE:
20  *
21  * Copyright (c) 1997-2006 Allan Kent
22  * All rights reserved.
23  *
24  * Redistribution and use in source and binary forms, with or without
25  * modification, are permitted under the terms of the BSD License.
26  *
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.
39  *
40  * @category   Date and Time
41  * @package    Date
42  * @author     Allan Kent <allan@lodestone.co.za>
43  * @copyright  1997-2006 Allan Kent
44  * @license    http://www.opensource.org/licenses/bsd-license.php
45  *             BSD License
46  * @version    CVS: $Id$
47  * @link       http://pear.php.net/package/Date
48  * @since      File available since Release 1.3
49  */
50
51 // }}}
52 // {{{ Class: Date_Human
53
54 /**
55  * Class to convert date strings between Gregorian and Human calendar formats
56  *
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
64  *
65  * @category  Date and Time
66  * @package   Date
67  * @author    Allan Kent <allan@lodestone.co.za>
68  * @copyright 1997-2005 Allan Kent
69  * @license   http://www.opensource.org/licenses/bsd-license.php
70  *            BSD License
71  * @version   Release: 1.5.0a4
72  * @link      http://pear.php.net/package/Date
73  * @since     Class available since Release 1.3
74  */
75 class Date_Human
76 {
77     // {{{ gregorianToHuman()
78
79     /**
80      * Returns an associative array containing the converted date information
81      * in 'Human Calendar' format.
82      *
83      * If the day is New Years Day, the function will return
84      * "hdom" =>  0
85      * "hdow" =>  0
86      * "hwom" =>  0
87      * "hwoy" =>  0
88      * "hmoy" => -1
89      *  Since 0 is a valid month number under the Human Calendar, I have left
90      *  the month as -1 for New Years Day.
91      *
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
95      *
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
102      *               )
103      * @access   public
104      * @static
105      */
106     public function gregorianToHuman($day = 0, $month = 0, $year = 0)
107     {
108         /*
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
113          */
114         if ((empty($day) || (empty($month)) || empty($year))) {
115             $dateinfo = getdate(time());
116         }
117         if (empty($day)) {
118             $day = $dateinfo["mday"];
119         }
120         if (empty($month)) {
121             $month = $dateinfo["mon"];
122         }
123         if (empty($year)) {
124             $year = $dateinfo["year"];
125         }
126         /*
127          * We need to know how many days into the year we are
128          */
129         $dateinfo = getdate(mktime(0, 0, 0, $month, $day, $year));
130         $dayofyear = $dateinfo["yday"];
131         /*
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
138          * gets around this.
139          */
140         $dayofyear--;
141         /*
142          * 28 days in a month...
143          */
144         $humanMonthOfYear = floor($dayofyear / 28);
145         /*
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.
148          */
149         if ($humanMonthOfYear == 0) {
150             $humanDayOfMonth = $dayofyear;
151         } else {
152             $humanDayOfMonth = ($dayofyear) % 28;
153         }
154         /*
155          * Day of the week is modulus 7
156          */
157         $humanDayOfWeek = $dayofyear % 7;
158         /*
159          * We can now increment $dayofyear back to it's correct value for
160          * the remainder of the calculations
161          */
162         $dayofyear++;
163         /*
164          * $humanDayOfMonth needs to be incremented now - recall that we fudged
165          * it a bit by decrementing $dayofyear earlier
166          * Same goes for $humanDayOfWeek
167          */
168         $humanDayOfMonth++;
169         $humanDayOfWeek++;
170         /*
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
173          */
174         $humanWeekOfMonth = ceil($humanDayOfMonth / 7);
175         $humanWeekOfYear = ceil($dayofyear / 7);
176         /*
177          * Return an associative array of the values
178          */
179         return array("hdom" => $humanDayOfMonth,
180             "hdow" => $humanDayOfWeek,
181             "hwom" => $humanWeekOfMonth,
182             "hwoy" => $humanWeekOfYear,
183             "hmoy" => $humanMonthOfYear);
184     }
185
186     // }}}
187     // {{{ humanToGregorian()
188
189     /**
190      * Returns unix timestamp for a given Human Calendar date
191      *
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
195      *
196      * @return   int unix timestamp of date
197      * @access   public
198      * @static
199      */
200     public function humanToGregorian($day, $month, $year = 0)
201     {
202         /*
203          * Check to see if the year has been passed through.
204          * If not get current year
205          */
206         if (empty($year)) {
207             $dateinfo = getdate(time());
208             $year = $dateinfo["year"];
209         }
210         /*
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
213          */
214         $DayOfYear = $month * 28;
215         $DayOfYear += $day;
216         /*
217          * Human Calendar starts at 0, so we need to increment $DayOfYear
218          * to take into account the day 00
219          */
220         $DayOfYear++;
221         /*
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
224          * will work fine.
225          */
226         $GregorianTimeStamp = mktime(0, 0, 0, 1, $DayOfYear, $year);
227         return $GregorianTimeStamp;
228     }
229
230     // }}}
231 }
232
233 // }}}
234
235 /*
236  * Local variables:
237  * mode: php
238  * tab-width: 4
239  * c-basic-offset: 4
240  * c-hanging-comment-ender-p: nil
241  * End:
242  */