1
|
|
|
<?php |
2
|
|
|
/* vim: set expandtab tabstop=4 shiftwidth=4: */ |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Contains the Calendar_Util_Textual class |
6
|
|
|
* |
7
|
|
|
* PHP versions 4 and 5 |
8
|
|
|
* |
9
|
|
|
* LICENSE: Redistribution and use in source and binary forms, with or without |
10
|
|
|
* modification, are permitted provided that the following conditions are met: |
11
|
|
|
* 1. Redistributions of source code must retain the above copyright |
12
|
|
|
* notice, this list of conditions and the following disclaimer. |
13
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
14
|
|
|
* notice, this list of conditions and the following disclaimer in the |
15
|
|
|
* documentation and/or other materials provided with the distribution. |
16
|
|
|
* 3. The name of the author may not be used to endorse or promote products |
17
|
|
|
* derived from this software without specific prior written permission. |
18
|
|
|
* |
19
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED |
20
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
21
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
22
|
|
|
* IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY |
23
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
24
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
25
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
26
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
28
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29
|
|
|
* |
30
|
|
|
* @category Date and Time |
31
|
|
|
* @package Calendar |
32
|
|
|
* @author Harry Fuecks <[email protected]> |
33
|
|
|
* @author Lorenzo Alberton <[email protected]> |
34
|
|
|
* @copyright 2003-2007 Harry Fuecks, Lorenzo Alberton |
35
|
|
|
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) |
36
|
|
|
* @version CVS: $Id$ |
37
|
|
|
* @link http://pear.php.net/package/Calendar |
38
|
|
|
*/ |
39
|
|
|
namespace Pear\Calendar\Util; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @package Calendar |
43
|
|
|
* @version $Id$ |
44
|
|
|
*/ |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Allows Calendar include path to be redefined |
48
|
|
|
* @ignore |
49
|
|
|
*/ |
50
|
|
|
if (!defined('CALENDAR_ROOT')) { |
51
|
|
|
define('CALENDAR_ROOT', 'Calendar'.DIRECTORY_SEPARATOR); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Load Calendar decorator base class |
56
|
|
|
*/ |
57
|
|
|
require_once CALENDAR_ROOT.'Decorator.php'; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Static utlities to help with fetching textual representations of months and |
61
|
|
|
* days of the week. |
62
|
|
|
* |
63
|
|
|
* @category Date and Time |
64
|
|
|
* @package Calendar |
65
|
|
|
* @author Harry Fuecks <[email protected]> |
66
|
|
|
* @author Lorenzo Alberton <[email protected]> |
67
|
|
|
* @copyright 2003-2007 Harry Fuecks, Lorenzo Alberton |
68
|
|
|
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) |
69
|
|
|
* @link http://pear.php.net/package/Calendar |
70
|
|
|
* @access public |
71
|
|
|
*/ |
72
|
|
|
class Calendar_Util_Textual |
73
|
|
|
{ |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns an array of 12 month names (first index = 1) |
77
|
|
|
* |
78
|
|
|
* @param string $format (optional) format of returned months (one|two|short|long) |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
* @access public |
82
|
|
|
* @static |
83
|
|
|
*/ |
84
|
|
|
function monthNames($format = 'long') |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$formats = array( |
87
|
|
|
'one' => '%b', |
88
|
|
|
'two' => '%b', |
89
|
|
|
'short' => '%b', |
90
|
|
|
'long' => '%B', |
91
|
|
|
); |
92
|
|
|
if (!array_key_exists($format, $formats)) { |
93
|
|
|
$format = 'long'; |
94
|
|
|
} |
95
|
|
|
$months = array(); |
96
|
|
|
for ($i=1; $i<=12; $i++) { |
97
|
|
|
$stamp = mktime(0, 0, 0, $i, 1, 2003); |
98
|
|
|
$month = strftime($formats[$format], $stamp); |
99
|
|
|
switch($format) { |
100
|
|
|
case 'one': |
101
|
|
|
$month = substr($month, 0, 1); |
102
|
|
|
break; |
103
|
|
|
case 'two': |
104
|
|
|
$month = substr($month, 0, 2); |
105
|
|
|
break; |
106
|
|
|
} |
107
|
|
|
$months[$i] = $month; |
108
|
|
|
} |
109
|
|
|
return $months; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Returns an array of 7 week day names (first index = 0) |
114
|
|
|
* |
115
|
|
|
* @param string $format (optional) format of returned days (one,two,short or long) |
116
|
|
|
* |
117
|
|
|
* @return array |
118
|
|
|
* @access public |
119
|
|
|
* @static |
120
|
|
|
*/ |
121
|
|
|
function weekdayNames($format = 'long') |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
$formats = array( |
124
|
|
|
'one' => '%a', |
125
|
|
|
'two' => '%a', |
126
|
|
|
'short' => '%a', |
127
|
|
|
'long' => '%A', |
128
|
|
|
); |
129
|
|
|
if (!array_key_exists($format, $formats)) { |
130
|
|
|
$format = 'long'; |
131
|
|
|
} |
132
|
|
|
$days = array(); |
133
|
|
|
for ($i=0; $i<=6; $i++) { |
134
|
|
|
$stamp = mktime(0, 0, 0, 11, $i+2, 2003); |
135
|
|
|
$day = strftime($formats[$format], $stamp); |
136
|
|
|
switch($format) { |
137
|
|
|
case 'one': |
138
|
|
|
$day = substr($day, 0, 1); |
139
|
|
|
break; |
140
|
|
|
case 'two': |
141
|
|
|
$day = substr($day, 0, 2); |
142
|
|
|
break; |
143
|
|
|
} |
144
|
|
|
$days[$i] = $day; |
145
|
|
|
} |
146
|
|
|
return $days; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Returns textual representation of the previous month of the decorated calendar object |
151
|
|
|
* |
152
|
|
|
* @param object $Calendar subclass of Calendar e.g. Calendar_Month |
153
|
|
|
* @param string $format (optional) format of returned months (one,two,short or long) |
154
|
|
|
* |
155
|
|
|
* @return string |
156
|
|
|
* @access public |
157
|
|
|
* @static |
158
|
|
|
*/ |
159
|
|
|
function prevMonthName($Calendar, $format = 'long') |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
$months = Calendar_Util_Textual::monthNames($format); |
|
|
|
|
162
|
|
|
return $months[$Calendar->prevMonth()]; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Returns textual representation of the month of the decorated calendar object |
167
|
|
|
* |
168
|
|
|
* @param object $Calendar subclass of Calendar e.g. Calendar_Month |
169
|
|
|
* @param string $format (optional) format of returned months (one,two,short or long) |
170
|
|
|
* |
171
|
|
|
* @return string |
172
|
|
|
* @access public |
173
|
|
|
* @static |
174
|
|
|
*/ |
175
|
|
|
function thisMonthName($Calendar, $format = 'long') |
|
|
|
|
176
|
|
|
{ |
177
|
|
|
$months = Calendar_Util_Textual::monthNames($format); |
|
|
|
|
178
|
|
|
return $months[$Calendar->thisMonth()]; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Returns textual representation of the next month of the decorated calendar object |
183
|
|
|
* |
184
|
|
|
* @param object $Calendar subclass of Calendar e.g. Calendar_Month |
185
|
|
|
* @param string $format (optional) format of returned months (one,two,short or long) |
186
|
|
|
* |
187
|
|
|
* @return string |
188
|
|
|
* @access public |
189
|
|
|
* @static |
190
|
|
|
*/ |
191
|
|
|
function nextMonthName($Calendar, $format = 'long') |
|
|
|
|
192
|
|
|
{ |
193
|
|
|
$months = Calendar_Util_Textual::monthNames($format); |
|
|
|
|
194
|
|
|
return $months[$Calendar->nextMonth()]; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Returns textual representation of the previous day of week of the decorated calendar object |
199
|
|
|
* <b>Note:</b> Requires PEAR::Date |
200
|
|
|
* |
201
|
|
|
* @param object $Calendar subclass of Calendar e.g. Calendar_Month |
202
|
|
|
* @param string $format (optional) format of returned months (one,two,short or long) |
203
|
|
|
* |
204
|
|
|
* @return string |
205
|
|
|
* @access public |
206
|
|
|
* @static |
207
|
|
|
*/ |
208
|
|
|
function prevDayName($Calendar, $format = 'long') |
|
|
|
|
209
|
|
|
{ |
210
|
|
|
$days = Calendar_Util_Textual::weekdayNames($format); |
|
|
|
|
211
|
|
|
$stamp = $Calendar->prevDay('timestamp'); |
212
|
|
|
$cE = $Calendar->getEngine(); |
213
|
|
|
include_once 'Date/Calc.php'; |
214
|
|
|
$day = Date_Calc::dayOfWeek($cE->stampToDay($stamp), |
|
|
|
|
215
|
|
|
$cE->stampToMonth($stamp), $cE->stampToYear($stamp)); |
216
|
|
|
return $days[$day]; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Returns textual representation of the day of week of the decorated calendar object |
221
|
|
|
* <b>Note:</b> Requires PEAR::Date |
222
|
|
|
* |
223
|
|
|
* @param object $Calendar subclass of Calendar e.g. Calendar_Month |
224
|
|
|
* @param string $format (optional) format of returned months (one,two,short or long) |
225
|
|
|
* |
226
|
|
|
* @return string |
227
|
|
|
* @access public |
228
|
|
|
* @static |
229
|
|
|
*/ |
230
|
|
|
function thisDayName($Calendar, $format='long') |
|
|
|
|
231
|
|
|
{ |
232
|
|
|
$days = Calendar_Util_Textual::weekdayNames($format); |
|
|
|
|
233
|
|
|
include_once 'Date/Calc.php'; |
234
|
|
|
$day = Date_Calc::dayOfWeek($Calendar->thisDay(), $Calendar->thisMonth(), $Calendar->thisYear()); |
235
|
|
|
return $days[$day]; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Returns textual representation of the next day of week of the decorated calendar object |
240
|
|
|
* |
241
|
|
|
* @param object $Calendar subclass of Calendar e.g. Calendar_Month |
242
|
|
|
* @param string $format (optional) format of returned months (one,two,short or long) |
243
|
|
|
* |
244
|
|
|
* @return string |
245
|
|
|
* @access public |
246
|
|
|
* @static |
247
|
|
|
*/ |
248
|
|
|
function nextDayName($Calendar, $format='long') |
|
|
|
|
249
|
|
|
{ |
250
|
|
|
$days = Calendar_Util_Textual::weekdayNames($format); |
|
|
|
|
251
|
|
|
$stamp = $Calendar->nextDay('timestamp'); |
252
|
|
|
$cE = $Calendar->getEngine(); |
253
|
|
|
include_once 'Date/Calc.php'; |
254
|
|
|
$day = Date_Calc::dayOfWeek($cE->stampToDay($stamp), |
255
|
|
|
$cE->stampToMonth($stamp), $cE->stampToYear($stamp)); |
256
|
|
|
return $days[$day]; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Returns the days of the week using the order defined in the decorated |
261
|
|
|
* calendar object. Only useful for Calendar_Month_Weekdays, Calendar_Month_Weeks |
262
|
|
|
* and Calendar_Week. Otherwise the returned array will begin on Sunday |
263
|
|
|
* |
264
|
|
|
* @param object $Calendar subclass of Calendar e.g. Calendar_Month |
265
|
|
|
* @param string $format (optional) format of returned months (one,two,short or long) |
266
|
|
|
* |
267
|
|
|
* @return array ordered array of week day names |
268
|
|
|
* @access public |
269
|
|
|
* @static |
270
|
|
|
*/ |
271
|
|
|
function orderedWeekdays($Calendar, $format = 'long') |
|
|
|
|
272
|
|
|
{ |
273
|
|
|
$days = Calendar_Util_Textual::weekdayNames($format); |
|
|
|
|
274
|
|
|
|
275
|
|
|
if (isset($Calendar->tableHelper)) { |
276
|
|
|
$ordereddays = $Calendar->tableHelper->getDaysOfWeek(); |
277
|
|
|
} else { |
278
|
|
|
//default: start from Sunday |
279
|
|
|
$firstDay = 0; |
280
|
|
|
//check if defined / set |
281
|
|
|
if (defined('CALENDAR_FIRST_DAY_OF_WEEK')) { |
282
|
|
|
$firstDay = CALENDAR_FIRST_DAY_OF_WEEK; |
283
|
|
|
} elseif(isset($Calendar->firstDay)) { |
284
|
|
|
$firstDay = $Calendar->firstDay; |
285
|
|
|
} |
286
|
|
|
$ordereddays = array(); |
287
|
|
|
for ($i = $firstDay; $i < 7; $i++) { |
288
|
|
|
$ordereddays[] = $i; |
289
|
|
|
} |
290
|
|
|
for ($i = 0; $i < $firstDay; $i++) { |
291
|
|
|
$ordereddays[] = $i; |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
$ordereddays = array_flip($ordereddays); |
296
|
|
|
$i = 0; |
297
|
|
|
$returndays = array(); |
298
|
|
|
foreach ($ordereddays as $key => $value) { |
299
|
|
|
$returndays[$i] = $days[$key]; |
300
|
|
|
$i++; |
301
|
|
|
} |
302
|
|
|
return $returndays; |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.