/[cvs]/nfo/php/libs/net.php.pear/Date/Human.php
ViewVC logotype

Contents of /nfo/php/libs/net.php.pear/Date/Human.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Tue Oct 29 19:11:41 2002 UTC (21 years, 8 months ago) by cvsjoko
Branch: MAIN
CVS Tags: HEAD
+ new pear-libraries

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

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed