Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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: 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
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
 * @author     Allan Kent <allan@lodestone.co.za>
66
 * @copyright  1997-2005 Allan Kent
67
 * @license    http://www.opensource.org/licenses/bsd-license.php
68
 *             BSD License
69
 * @version    Release: 1.4.7
70
 * @link       http://pear.php.net/package/Date
71
 * @since      Class available since Release 1.3
72
 */
73
class Date_Human
74
{
75
    // {{{ gregorianToHuman()
76
 
77
    /**
78
     * Returns an associative array containing the converted date information
79
     * in 'Human Calendar' format.
80
     *
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
84
     *
85
     * @access public
86
     *
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
93
     *               )
94
     *
95
     * If the day is New Years Day, the function will return
96
     * "hdom" =>  0
97
     * "hdow" =>  0
98
     * "hwom" =>  0
99
     * "hwoy" =>  0
100
     * "hmoy" => -1
101
     *  Since 0 is a valid month number under the Human Calendar, I have left
102
     *  the month as -1 for New Years Day.
103
     */
104
    function gregorianToHuman($day=0, $month=0, $year=0)
105
    {
106
        /*
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
111
         */
112
        if ((empty($day) || (empty($month)) || empty($year))) {
113
            $dateinfo = getdate(time());
114
        }
115
        if (empty($day)) {
116
            $day = $dateinfo["mday"];
117
        }
118
        if (empty($month)) {
119
            $month = $dateinfo["mon"];
120
        }
121
        if (empty($year)) {
122
            $year = $dateinfo["year"];
123
        }
124
        /*
125
         * We need to know how many days into the year we are
126
         */
127
        $dateinfo = getdate(mktime(0, 0, 0, $month, $day, $year));
128
        $dayofyear = $dateinfo["yday"];
129
        /*
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
136
         * gets around this.
137
         */
138
        $dayofyear--;
139
        /*
140
         * 28 days in a month...
141
         */
142
        $humanMonthOfYear = floor($dayofyear / 28);
143
        /*
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.
146
         */
147
        if ($humanMonthOfYear == 0) {
148
            $humanDayOfMonth = $dayofyear;
149
        } else {
150
            $humanDayOfMonth = ($dayofyear) % 28;
151
        }
152
        /*
153
         * Day of the week is modulus 7
154
         */
155
        $humanDayOfWeek = $dayofyear % 7;
156
        /*
157
         * We can now increment $dayofyear back to it's correct value for
158
         * the remainder of the calculations
159
         */
160
        $dayofyear++;
161
        /*
162
         * $humanDayOfMonth needs to be incremented now - recall that we fudged
163
         * it a bit by decrementing $dayofyear earlier
164
         * Same goes for $humanDayOfWeek
165
         */
166
        $humanDayOfMonth++;
167
        $humanDayOfWeek++;
168
        /*
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
171
         */
172
        $humanWeekOfMonth = ceil($humanDayOfMonth / 7);
173
        $humanWeekOfYear = ceil($dayofyear / 7);
174
        /*
175
         * Return an associative array of the values
176
         */
177
        return array(
178
                     "hdom" => $humanDayOfMonth,
179
                     "hdow" => $humanDayOfWeek,
180
                     "hwom" => $humanWeekOfMonth,
181
                     "hwoy" => $humanWeekOfYear,
182
                     "hmoy" => $humanMonthOfYear );
183
    }
184
 
185
    // }}}
186
    // {{{ humanToGregorian()
187
 
188
    /**
189
     * Returns unix timestamp for a given Human Calendar date
190
     *
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
194
     *
195
     * @access public
196
     *
197
     * @return int unix timestamp of date
198
     */
199
    function humanToGregorian($day, $month, $year=0)
200
    {
201
        /*
202
         * Check to see if the year has been passed through.
203
         * If not get current year
204
         */
205
        if (empty($year)) {
206
            $dateinfo = getdate(time());
207
            $year = $dateinfo["year"];
208
        }
209
        /*
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
212
         */
213
        $DayOfYear = $month * 28;
214
        $DayOfYear += $day;
215
        /*
216
         * Human Calendar starts at 0, so we need to increment $DayOfYear
217
         * to take into account the day 00
218
         */
219
        $DayOfYear++;
220
        /*
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
223
         * will work fine.
224
         */
225
        $GregorianTimeStamp = mktime(0, 0, 0, 1, $DayOfYear, $year);
226
        return $GregorianTimeStamp;
227
    }
228
 
229
    // }}}
230
}
231
 
232
// }}}
233
 
234
/*
235
 * Local variables:
236
 * mode: php
237
 * tab-width: 4
238
 * c-basic-offset: 4
239
 * c-hanging-comment-ender-p: nil
240
 * End:
241
 */
242
?>