|
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
|
|
|
* Static utlities to help with fetching textual representations of months and |
|
48
|
|
|
* days of the week. |
|
49
|
|
|
* |
|
50
|
|
|
* @category Date and Time |
|
51
|
|
|
* @package Calendar |
|
52
|
|
|
* @author Harry Fuecks <[email protected]> |
|
53
|
|
|
* @author Lorenzo Alberton <[email protected]> |
|
54
|
|
|
* @copyright 2003-2007 Harry Fuecks, Lorenzo Alberton |
|
55
|
|
|
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) |
|
56
|
|
|
* @link http://pear.php.net/package/Calendar |
|
57
|
|
|
* @access public |
|
58
|
|
|
*/ |
|
59
|
|
|
class Textual |
|
60
|
|
|
{ |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Returns an array of 12 month names (first index = 1) |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $format (optional) format of returned months (one|two|short|long) |
|
66
|
|
|
* |
|
67
|
|
|
* @return array |
|
68
|
|
|
* @access public |
|
69
|
|
|
* @static |
|
70
|
|
|
*/ |
|
71
|
|
|
function monthNames($format = 'long') |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
|
|
$formats = array( |
|
74
|
|
|
'one' => '%b', |
|
75
|
|
|
'two' => '%b', |
|
76
|
|
|
'short' => '%b', |
|
77
|
|
|
'long' => '%B', |
|
78
|
|
|
); |
|
79
|
|
|
if (!array_key_exists($format, $formats)) { |
|
80
|
|
|
$format = 'long'; |
|
81
|
|
|
} |
|
82
|
|
|
$months = array(); |
|
83
|
|
|
for ($i=1; $i<=12; $i++) { |
|
84
|
|
|
$stamp = mktime(0, 0, 0, $i, 1, 2003); |
|
85
|
|
|
$month = strftime($formats[$format], $stamp); |
|
86
|
|
|
switch($format) { |
|
87
|
|
|
case 'one': |
|
88
|
|
|
$month = substr($month, 0, 1); |
|
89
|
|
|
break; |
|
90
|
|
|
case 'two': |
|
91
|
|
|
$month = substr($month, 0, 2); |
|
92
|
|
|
break; |
|
93
|
|
|
} |
|
94
|
|
|
$months[$i] = $month; |
|
95
|
|
|
} |
|
96
|
|
|
return $months; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Returns an array of 7 week day names (first index = 0) |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $format (optional) format of returned days (one,two,short or long) |
|
103
|
|
|
* |
|
104
|
|
|
* @return array |
|
105
|
|
|
* @access public |
|
106
|
|
|
* @static |
|
107
|
|
|
*/ |
|
108
|
|
|
function weekdayNames($format = 'long') |
|
|
|
|
|
|
109
|
|
|
{ |
|
110
|
|
|
$formats = array( |
|
111
|
|
|
'one' => '%a', |
|
112
|
|
|
'two' => '%a', |
|
113
|
|
|
'short' => '%a', |
|
114
|
|
|
'long' => '%A', |
|
115
|
|
|
); |
|
116
|
|
|
if (!array_key_exists($format, $formats)) { |
|
117
|
|
|
$format = 'long'; |
|
118
|
|
|
} |
|
119
|
|
|
$days = array(); |
|
120
|
|
|
for ($i=0; $i<=6; $i++) { |
|
121
|
|
|
$stamp = mktime(0, 0, 0, 11, $i+2, 2003); |
|
122
|
|
|
$day = strftime($formats[$format], $stamp); |
|
123
|
|
|
switch($format) { |
|
124
|
|
|
case 'one': |
|
125
|
|
|
$day = substr($day, 0, 1); |
|
126
|
|
|
break; |
|
127
|
|
|
case 'two': |
|
128
|
|
|
$day = substr($day, 0, 2); |
|
129
|
|
|
break; |
|
130
|
|
|
} |
|
131
|
|
|
$days[$i] = $day; |
|
132
|
|
|
} |
|
133
|
|
|
return $days; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Returns textual representation of the previous month of the decorated calendar object |
|
138
|
|
|
* |
|
139
|
|
|
* @param object $Calendar subclass of Calendar e.g. Calendar_Month |
|
140
|
|
|
* @param string $format (optional) format of returned months (one,two,short or long) |
|
141
|
|
|
* |
|
142
|
|
|
* @return string |
|
143
|
|
|
* @access public |
|
144
|
|
|
* @static |
|
145
|
|
|
*/ |
|
146
|
|
|
function prevMonthName($Calendar, $format = 'long') |
|
|
|
|
|
|
147
|
|
|
{ |
|
148
|
|
|
$months = Textual::monthNames($format); |
|
|
|
|
|
|
149
|
|
|
return $months[$Calendar->prevMonth()]; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Returns textual representation of the month of the decorated calendar object |
|
154
|
|
|
* |
|
155
|
|
|
* @param object $Calendar subclass of Calendar e.g. Calendar_Month |
|
156
|
|
|
* @param string $format (optional) format of returned months (one,two,short or long) |
|
157
|
|
|
* |
|
158
|
|
|
* @return string |
|
159
|
|
|
* @access public |
|
160
|
|
|
* @static |
|
161
|
|
|
*/ |
|
162
|
|
|
function thisMonthName($Calendar, $format = 'long') |
|
|
|
|
|
|
163
|
|
|
{ |
|
164
|
|
|
$months = Textual::monthNames($format); |
|
|
|
|
|
|
165
|
|
|
return $months[$Calendar->thisMonth()]; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Returns textual representation of the next month of the decorated calendar object |
|
170
|
|
|
* |
|
171
|
|
|
* @param object $Calendar subclass of Calendar e.g. Calendar_Month |
|
172
|
|
|
* @param string $format (optional) format of returned months (one,two,short or long) |
|
173
|
|
|
* |
|
174
|
|
|
* @return string |
|
175
|
|
|
* @access public |
|
176
|
|
|
* @static |
|
177
|
|
|
*/ |
|
178
|
|
|
function nextMonthName($Calendar, $format = 'long') |
|
|
|
|
|
|
179
|
|
|
{ |
|
180
|
|
|
$months = Textual::monthNames($format); |
|
|
|
|
|
|
181
|
|
|
return $months[$Calendar->nextMonth()]; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Returns textual representation of the previous day of week of the decorated calendar object |
|
186
|
|
|
* <b>Note:</b> Requires PEAR::Date |
|
187
|
|
|
* |
|
188
|
|
|
* @param object $Calendar subclass of Calendar e.g. Calendar_Month |
|
189
|
|
|
* @param string $format (optional) format of returned months (one,two,short or long) |
|
190
|
|
|
* |
|
191
|
|
|
* @return string |
|
192
|
|
|
* @access public |
|
193
|
|
|
* @static |
|
194
|
|
|
*/ |
|
195
|
|
|
function prevDayName($Calendar, $format = 'long') |
|
|
|
|
|
|
196
|
|
|
{ |
|
197
|
|
|
$days = Textual::weekdayNames($format); |
|
|
|
|
|
|
198
|
|
|
$stamp = $Calendar->prevDay('timestamp'); |
|
199
|
|
|
$date = date_create($stamp); |
|
200
|
|
|
$day = date_format($date, 'w'); |
|
201
|
|
|
return $days[$day]; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Returns textual representation of the day of week of the decorated calendar object |
|
206
|
|
|
* <b>Note:</b> Requires PEAR::Date |
|
207
|
|
|
* |
|
208
|
|
|
* @param object $Calendar subclass of Calendar e.g. Calendar_Month |
|
209
|
|
|
* @param string $format (optional) format of returned months (one,two,short or long) |
|
210
|
|
|
* |
|
211
|
|
|
* @return string |
|
212
|
|
|
* @access public |
|
213
|
|
|
* @static |
|
214
|
|
|
*/ |
|
215
|
|
|
function thisDayName($Calendar, $format='long') |
|
|
|
|
|
|
216
|
|
|
{ |
|
217
|
|
|
$days = Textual::weekdayNames($format); |
|
|
|
|
|
|
218
|
|
|
$stamp = $Calendar->thisDay('timestamp'); |
|
219
|
|
|
$date = date_create($stamp); |
|
220
|
|
|
$day = date_format($date, 'w'); |
|
221
|
|
|
return $days[$day]; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Returns textual representation of the next day of week of the decorated calendar object |
|
226
|
|
|
* |
|
227
|
|
|
* @param object $Calendar subclass of Calendar e.g. Calendar_Month |
|
228
|
|
|
* @param string $format (optional) format of returned months (one,two,short or long) |
|
229
|
|
|
* |
|
230
|
|
|
* @return string |
|
231
|
|
|
* @access public |
|
232
|
|
|
* @static |
|
233
|
|
|
*/ |
|
234
|
|
|
function nextDayName($Calendar, $format='long') |
|
|
|
|
|
|
235
|
|
|
{ |
|
236
|
|
|
$days = Textual::weekdayNames($format); |
|
|
|
|
|
|
237
|
|
|
$stamp = $Calendar->nextDay('timestamp'); |
|
238
|
|
|
$date = date_create($stamp); |
|
239
|
|
|
$day = date_format($date, 'w'); |
|
240
|
|
|
return $days[$day]; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Returns the days of the week using the order defined in the decorated |
|
245
|
|
|
* calendar object. Only useful for Calendar_Month_Weekdays, Calendar_Month_Weeks |
|
246
|
|
|
* and Calendar_Week. Otherwise the returned array will begin on Sunday |
|
247
|
|
|
* |
|
248
|
|
|
* @param object $Calendar subclass of Calendar e.g. Calendar_Month |
|
249
|
|
|
* @param string $format (optional) format of returned months (one,two,short or long) |
|
250
|
|
|
* |
|
251
|
|
|
* @return array ordered array of week day names |
|
252
|
|
|
* @access public |
|
253
|
|
|
* @static |
|
254
|
|
|
*/ |
|
255
|
|
|
function orderedWeekdays($Calendar, $format = 'long') |
|
|
|
|
|
|
256
|
|
|
{ |
|
257
|
|
|
$days = Textual::weekdayNames($format); |
|
|
|
|
|
|
258
|
|
|
|
|
259
|
|
|
if (isset($Calendar->tableHelper)) { |
|
260
|
|
|
$ordereddays = $Calendar->tableHelper->getDaysOfWeek(); |
|
261
|
|
|
} else { |
|
262
|
|
|
//default: start from Sunday |
|
263
|
|
|
$firstDay = 0; |
|
264
|
|
|
//check if defined / set |
|
265
|
|
|
if (defined('CALENDAR_FIRST_DAY_OF_WEEK')) { |
|
266
|
|
|
$firstDay = CALENDAR_FIRST_DAY_OF_WEEK; |
|
267
|
|
|
} elseif(isset($Calendar->firstDay)) { |
|
268
|
|
|
$firstDay = $Calendar->firstDay; |
|
269
|
|
|
} |
|
270
|
|
|
$ordereddays = array(); |
|
271
|
|
|
for ($i = $firstDay; $i < 7; $i++) { |
|
272
|
|
|
$ordereddays[] = $i; |
|
273
|
|
|
} |
|
274
|
|
|
for ($i = 0; $i < $firstDay; $i++) { |
|
275
|
|
|
$ordereddays[] = $i; |
|
276
|
|
|
} |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
$ordereddays = array_flip($ordereddays); |
|
280
|
|
|
$i = 0; |
|
281
|
|
|
$returndays = array(); |
|
282
|
|
|
foreach ($ordereddays as $key => $value) { |
|
283
|
|
|
$returndays[$i] = $days[$key]; |
|
284
|
|
|
$i++; |
|
285
|
|
|
} |
|
286
|
|
|
return $returndays; |
|
287
|
|
|
} |
|
288
|
|
|
} |
|
289
|
|
|
|
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.