1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
599a39cd
|
Assos Assos
|
|
3 |
85ad3d82
|
Assos Assos
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Code to compute the dates that match an iCal RRULE.
|
6 |
|
|
*
|
7 |
|
|
* Moved to a separate file since it is not used on most pages
|
8 |
|
|
* so the code is not parsed unless needed.
|
9 |
|
|
*
|
10 |
|
|
* Extensive simpletests have been created to test the RRULE calculation
|
11 |
|
|
* results against official examples from RFC 2445.
|
12 |
|
|
*
|
13 |
|
|
* These calculations are expensive and results should be stored or cached
|
14 |
|
|
* so the calculation code is not called more often than necessary.
|
15 |
|
|
*
|
16 |
|
|
* Currently implemented:
|
17 |
|
|
* INTERVAL, UNTIL, COUNT, EXDATE, RDATE, BYDAY, BYMONTHDAY, BYMONTH,
|
18 |
|
|
* YEARLY, MONTHLY, WEEKLY, DAILY
|
19 |
|
|
*
|
20 |
|
|
* Currently not implemented:
|
21 |
|
|
*
|
22 |
|
|
* BYYEARDAY, MINUTELY, HOURLY, SECONDLY, BYMINUTE, BYHOUR, BYSECOND
|
23 |
|
|
* These could be implemented in the future.
|
24 |
|
|
*
|
25 |
|
|
* BYSETPOS
|
26 |
|
|
* Seldom used anywhere, so no reason to complicated the code.
|
27 |
|
|
*/
|
28 |
|
|
|
29 |
|
|
/**
|
30 |
|
|
* Private implementation of date_repeat_calc().
|
31 |
|
|
*
|
32 |
|
|
* Compute dates that match the requested rule, within a specified date range.
|
33 |
|
|
*/
|
34 |
|
|
function _date_repeat_calc($rrule, $start, $end, $exceptions, $timezone, $additions) {
|
35 |
|
|
module_load_include('inc', 'date_api', 'date_api_ical');
|
36 |
|
|
|
37 |
|
|
if (empty($timezone)) {
|
38 |
|
|
$timezone = date_default_timezone();
|
39 |
|
|
}
|
40 |
|
|
|
41 |
|
|
// Make sure the 'EXCEPTIONS' string isn't appended to the rule.
|
42 |
|
|
$parts = explode("\n", $rrule);
|
43 |
|
|
if (count($parts)) {
|
44 |
|
|
$rrule = $parts[0];
|
45 |
|
|
}
|
46 |
|
|
// Get the parsed array of rule values.
|
47 |
|
|
$rrule = date_ical_parse_rrule('RRULE:', $rrule);
|
48 |
|
|
|
49 |
|
|
// These default values indicate there is no RRULE here.
|
50 |
|
|
if ($rrule['FREQ'] == 'NONE' || (isset($rrule['INTERVAL']) && $rrule['INTERVAL'] == 0)) {
|
51 |
|
|
return array();
|
52 |
|
|
}
|
53 |
|
|
|
54 |
|
|
// Create a date object for the start and end dates.
|
55 |
|
|
$start_date = new DateObject($start, $timezone);
|
56 |
|
|
|
57 |
599a39cd
|
Assos Assos
|
// Versions of PHP greater than PHP 5.3.5 require that we set an explicit
|
58 |
|
|
// time when using date_modify() or the time may not match the original value.
|
59 |
|
|
// Adding this modifier gives us the same results in both older and newer
|
60 |
|
|
// versions of PHP.
|
61 |
85ad3d82
|
Assos Assos
|
$modify_time = ' ' . $start_date->format('g:ia');
|
62 |
|
|
|
63 |
|
|
// If the rule has an UNTIL, see if that is earlier than the end date.
|
64 |
|
|
if (!empty($rrule['UNTIL'])) {
|
65 |
|
|
$end_date = new DateObject($end, $timezone);
|
66 |
|
|
$until_date = date_ical_date($rrule['UNTIL'], $timezone);
|
67 |
|
|
if (date_format($until_date, 'U') < date_format($end_date, 'U')) {
|
68 |
|
|
$end_date = $until_date;
|
69 |
|
|
}
|
70 |
|
|
}
|
71 |
|
|
// The only valid option for an empty end date is when we have a count.
|
72 |
|
|
elseif (empty($end)) {
|
73 |
|
|
if (!empty($rrule['COUNT'])) {
|
74 |
|
|
$end_date = NULL;
|
75 |
|
|
}
|
76 |
|
|
else {
|
77 |
|
|
return array();
|
78 |
|
|
}
|
79 |
|
|
}
|
80 |
|
|
else {
|
81 |
|
|
$end_date = new DateObject($end, $timezone);
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
// Get an integer value for the interval, if none given, '1' is implied.
|
85 |
|
|
if (empty($rrule['INTERVAL'])) {
|
86 |
|
|
$rrule['INTERVAL'] = 1;
|
87 |
|
|
}
|
88 |
|
|
$interval = max(1, $rrule['INTERVAL']);
|
89 |
|
|
$count = isset($rrule['COUNT']) ? $rrule['COUNT'] : NULL;
|
90 |
|
|
|
91 |
|
|
if (empty($rrule['FREQ'])) {
|
92 |
|
|
$rrule['FREQ'] = 'DAILY';
|
93 |
|
|
}
|
94 |
|
|
|
95 |
|
|
// Make sure DAILY frequency isn't used in places it won't work;
|
96 |
b720ea3e
|
Assos Assos
|
if (!empty($rrule['BYMONTHDAY']) &&
|
97 |
|
|
!in_array($rrule['FREQ'], array('MONTHLY', 'YEARLY'))) {
|
98 |
85ad3d82
|
Assos Assos
|
$rrule['FREQ'] = 'MONTHLY';
|
99 |
|
|
}
|
100 |
b720ea3e
|
Assos Assos
|
elseif (!empty($rrule['BYDAY'])
|
101 |
|
|
&& !in_array($rrule['FREQ'], array('MONTHLY', 'WEEKLY', 'YEARLY'))) {
|
102 |
85ad3d82
|
Assos Assos
|
$rrule['FREQ'] = 'WEEKLY';
|
103 |
b720ea3e
|
Assos Assos
|
}
|
104 |
85ad3d82
|
Assos Assos
|
|
105 |
|
|
// Find the time period to jump forward between dates.
|
106 |
|
|
switch ($rrule['FREQ']) {
|
107 |
b720ea3e
|
Assos Assos
|
case 'DAILY':
|
108 |
|
|
$jump = $interval . ' days';
|
109 |
|
|
break;
|
110 |
|
|
|
111 |
|
|
case 'WEEKLY':
|
112 |
|
|
$jump = $interval . ' weeks';
|
113 |
|
|
break;
|
114 |
|
|
|
115 |
|
|
case 'MONTHLY':
|
116 |
|
|
$jump = $interval . ' months';
|
117 |
|
|
break;
|
118 |
|
|
|
119 |
|
|
case 'YEARLY':
|
120 |
|
|
$jump = $interval . ' years';
|
121 |
|
|
break;
|
122 |
85ad3d82
|
Assos Assos
|
}
|
123 |
|
|
$rrule = date_repeat_adjust_rrule($rrule, $start_date);
|
124 |
|
|
|
125 |
599a39cd
|
Assos Assos
|
// The start date always goes into the results, whether or not it meets the
|
126 |
|
|
// rules. RFC 2445 includes examples where the start date DOES NOT meet the
|
127 |
|
|
// rules, but the expected results always include the start date.
|
128 |
85ad3d82
|
Assos Assos
|
$days = array(date_format($start_date, DATE_FORMAT_DATETIME));
|
129 |
|
|
|
130 |
|
|
// BYMONTHDAY will look for specific days of the month in one or more months.
|
131 |
|
|
// This process is only valid when frequency is monthly or yearly.
|
132 |
|
|
if (!empty($rrule['BYMONTHDAY'])) {
|
133 |
|
|
$finished = FALSE;
|
134 |
599a39cd
|
Assos Assos
|
$current_day = clone $start_date;
|
135 |
85ad3d82
|
Assos Assos
|
$direction_days = array();
|
136 |
|
|
// Deconstruct the day in case it has a negative modifier.
|
137 |
|
|
foreach ($rrule['BYMONTHDAY'] as $day) {
|
138 |
|
|
preg_match("@(-)?([0-9]{1,2})@", $day, $regs);
|
139 |
|
|
if (!empty($regs[2])) {
|
140 |
|
|
// Convert parameters into full day name, count, and direction.
|
141 |
|
|
$direction_days[$day] = array(
|
142 |
|
|
'direction' => !empty($regs[1]) ? $regs[1] : '+',
|
143 |
|
|
'direction_count' => $regs[2],
|
144 |
b720ea3e
|
Assos Assos
|
);
|
145 |
85ad3d82
|
Assos Assos
|
}
|
146 |
|
|
}
|
147 |
|
|
while (!$finished) {
|
148 |
|
|
$period_finished = FALSE;
|
149 |
|
|
while (!$period_finished) {
|
150 |
|
|
foreach ($rrule['BYMONTHDAY'] as $monthday) {
|
151 |
|
|
$day = $direction_days[$monthday];
|
152 |
|
|
$current_day = date_repeat_set_month_day($current_day, NULL, $day['direction_count'], $day['direction'], $timezone, $modify_time);
|
153 |
|
|
date_repeat_add_dates($days, $current_day, $start_date, $end_date, $exceptions, $rrule);
|
154 |
|
|
if ($finished = date_repeat_is_finished($current_day, $days, $count, $end_date)) {
|
155 |
|
|
$period_finished = TRUE;
|
156 |
|
|
}
|
157 |
|
|
}
|
158 |
|
|
// If it's monthly, keep looping through months, one INTERVAL at a time.
|
159 |
|
|
if ($rrule['FREQ'] == 'MONTHLY') {
|
160 |
|
|
if ($finished = date_repeat_is_finished($current_day, $days, $count, $end_date)) {
|
161 |
|
|
$period_finished = TRUE;
|
162 |
|
|
}
|
163 |
|
|
// Back up to first of month and jump.
|
164 |
|
|
$current_day = date_repeat_set_month_day($current_day, NULL, 1, '+', $timezone, $modify_time);
|
165 |
|
|
date_modify($current_day, '+' . $jump . $modify_time);
|
166 |
|
|
}
|
167 |
|
|
// If it's yearly, break out of the loop at the
|
168 |
|
|
// end of every year, and jump one INTERVAL in years.
|
169 |
|
|
else {
|
170 |
|
|
if (date_format($current_day, 'n') == 12) {
|
171 |
|
|
$period_finished = TRUE;
|
172 |
|
|
}
|
173 |
|
|
else {
|
174 |
|
|
// Back up to first of month and jump.
|
175 |
|
|
$current_day = date_repeat_set_month_day($current_day, NULL, 1, '+', $timezone, $modify_time);
|
176 |
|
|
date_modify($current_day, '+1 month' . $modify_time);
|
177 |
|
|
}
|
178 |
|
|
}
|
179 |
|
|
}
|
180 |
|
|
if ($rrule['FREQ'] == 'YEARLY') {
|
181 |
|
|
// Back up to first of year and jump.
|
182 |
db9ffd17
|
Assos Assos
|
$current_day = date_repeat_set_year_day($current_day, NULL, NULL, 1, '+', $timezone, $modify_time);
|
183 |
85ad3d82
|
Assos Assos
|
date_modify($current_day, '+' . $jump . $modify_time);
|
184 |
|
|
}
|
185 |
|
|
$finished = date_repeat_is_finished($current_day, $days, $count, $end_date);
|
186 |
|
|
}
|
187 |
|
|
}
|
188 |
|
|
|
189 |
599a39cd
|
Assos Assos
|
// This is the simple fallback case, not looking for any BYDAY, just
|
190 |
|
|
// repeating the start date. Because of imputed BYDAY above, this will only
|
191 |
|
|
// test TRUE for a DAILY or less frequency (like HOURLY).
|
192 |
85ad3d82
|
Assos Assos
|
elseif (empty($rrule['BYDAY'])) {
|
193 |
|
|
// $current_day will keep track of where we are in the calculation.
|
194 |
599a39cd
|
Assos Assos
|
$current_day = clone $start_date;
|
195 |
85ad3d82
|
Assos Assos
|
$finished = FALSE;
|
196 |
|
|
$months = !empty($rrule['BYMONTH']) ? $rrule['BYMONTH'] : array();
|
197 |
|
|
while (!$finished) {
|
198 |
|
|
date_repeat_add_dates($days, $current_day, $start_date, $end_date, $exceptions, $rrule);
|
199 |
|
|
$finished = date_repeat_is_finished($current_day, $days, $count, $end_date);
|
200 |
|
|
date_modify($current_day, '+' . $jump . $modify_time);
|
201 |
|
|
}
|
202 |
|
|
}
|
203 |
|
|
|
204 |
|
|
else {
|
205 |
599a39cd
|
Assos Assos
|
// More complex searches for day names and criteria like '-1SU' or
|
206 |
|
|
// '2TU,2TH', require that we interate through the whole time period
|
207 |
|
|
// checking each BYDAY. Create helper array to pull day names out of iCal
|
208 |
|
|
// day strings.
|
209 |
85ad3d82
|
Assos Assos
|
$day_names = date_repeat_dow_day_options(FALSE);
|
210 |
|
|
$days_of_week = array_keys($day_names);
|
211 |
|
|
|
212 |
599a39cd
|
Assos Assos
|
// Parse out information about the BYDAYs and separate them depending on
|
213 |
|
|
// whether they have directional parameters like -1SU or 2TH.
|
214 |
85ad3d82
|
Assos Assos
|
$month_days = array();
|
215 |
|
|
$week_days = array();
|
216 |
|
|
|
217 |
|
|
// Find the right first day of the week to use, iCal rules say Monday
|
218 |
|
|
// should be used if none is specified.
|
219 |
|
|
$week_start_rule = !empty($rrule['WKST']) ? trim($rrule['WKST']) : 'MO';
|
220 |
|
|
$week_start_day = $day_names[$week_start_rule];
|
221 |
|
|
|
222 |
599a39cd
|
Assos Assos
|
// Make sure the week days array is sorted into week order, we use the
|
223 |
|
|
// $ordered_keys to get the right values into the key and force the array
|
224 |
|
|
// to that order. Needed later when we iterate through each week looking
|
225 |
|
|
// for days so we don't jump to the next week when we hit a day out of
|
226 |
|
|
// order.
|
227 |
85ad3d82
|
Assos Assos
|
$ordered = date_repeat_days_ordered($week_start_rule);
|
228 |
|
|
$ordered_keys = array_flip($ordered);
|
229 |
|
|
|
230 |
db9ffd17
|
Assos Assos
|
if ($rrule['FREQ'] == 'YEARLY' && !empty($rrule['BYMONTH'])) {
|
231 |
|
|
// Additional cycle to apply month preferences.
|
232 |
|
|
foreach ($rrule['BYMONTH'] as $month) {
|
233 |
|
|
foreach ($rrule['BYDAY'] as $day) {
|
234 |
|
|
preg_match("@(-)?([0-9]+)?([SU|MO|TU|WE|TH|FR|SA]{2})@", trim($day), $regs);
|
235 |
599a39cd
|
Assos Assos
|
// Convert parameters into full day name, count, and direction. Add
|
236 |
|
|
// leading zero to first 9 months.
|
237 |
db9ffd17
|
Assos Assos
|
if (!empty($regs[2])) {
|
238 |
|
|
$direction_days[] = array(
|
239 |
|
|
'day' => $day_names[$regs[3]],
|
240 |
|
|
'direction' => !empty($regs[1]) ? $regs[1] : '+',
|
241 |
|
|
'direction_count' => $regs[2],
|
242 |
|
|
'month' => strlen($month) > 1 ? $month : '0' . $month,
|
243 |
|
|
);
|
244 |
|
|
}
|
245 |
|
|
else {
|
246 |
|
|
$week_days[$ordered_keys[$regs[3]]] = $day_names[$regs[3]];
|
247 |
|
|
}
|
248 |
|
|
}
|
249 |
85ad3d82
|
Assos Assos
|
}
|
250 |
db9ffd17
|
Assos Assos
|
}
|
251 |
|
|
else {
|
252 |
|
|
foreach ($rrule['BYDAY'] as $day) {
|
253 |
|
|
preg_match("@(-)?([0-9]+)?([SU|MO|TU|WE|TH|FR|SA]{2})@", trim($day), $regs);
|
254 |
|
|
if (!empty($regs[2])) {
|
255 |
|
|
// Convert parameters into full day name, count, and direction.
|
256 |
|
|
$direction_days[] = array(
|
257 |
|
|
'day' => $day_names[$regs[3]],
|
258 |
|
|
'direction' => !empty($regs[1]) ? $regs[1] : '+',
|
259 |
|
|
'direction_count' => $regs[2],
|
260 |
|
|
'month' => NULL,
|
261 |
|
|
);
|
262 |
|
|
}
|
263 |
|
|
else {
|
264 |
|
|
$week_days[$ordered_keys[$regs[3]]] = $day_names[$regs[3]];
|
265 |
|
|
}
|
266 |
85ad3d82
|
Assos Assos
|
}
|
267 |
|
|
}
|
268 |
|
|
ksort($week_days);
|
269 |
|
|
|
270 |
599a39cd
|
Assos Assos
|
// BYDAYs with parameters like -1SU (last Sun) or 2TH (second Thur) need to
|
271 |
|
|
// be processed one month or year at a time.
|
272 |
85ad3d82
|
Assos Assos
|
if (!empty($direction_days) && in_array($rrule['FREQ'], array('MONTHLY', 'YEARLY'))) {
|
273 |
|
|
$finished = FALSE;
|
274 |
599a39cd
|
Assos Assos
|
$current_day = clone $start_date;
|
275 |
85ad3d82
|
Assos Assos
|
while (!$finished) {
|
276 |
|
|
foreach ($direction_days as $day) {
|
277 |
|
|
// Find the BYDAY date in the current month.
|
278 |
|
|
if ($rrule['FREQ'] == 'MONTHLY') {
|
279 |
|
|
$current_day = date_repeat_set_month_day($current_day, $day['day'], $day['direction_count'], $day['direction'], $timezone, $modify_time);
|
280 |
|
|
}
|
281 |
|
|
else {
|
282 |
db9ffd17
|
Assos Assos
|
$current_day = date_repeat_set_year_day($current_day, $day['month'], $day['day'], $day['direction_count'], $day['direction'], $timezone, $modify_time);
|
283 |
85ad3d82
|
Assos Assos
|
}
|
284 |
|
|
date_repeat_add_dates($days, $current_day, $start_date, $end_date, $exceptions, $rrule);
|
285 |
|
|
}
|
286 |
|
|
$finished = date_repeat_is_finished($current_day, $days, $count, $end_date);
|
287 |
599a39cd
|
Assos Assos
|
// Reset to beginning of period before jumping to next period. Needed
|
288 |
|
|
// especially when working with values like 'last Saturday' to be sure
|
289 |
|
|
// we don't skip months like February.
|
290 |
85ad3d82
|
Assos Assos
|
$year = date_format($current_day, 'Y');
|
291 |
|
|
$month = date_format($current_day, 'n');
|
292 |
|
|
if ($rrule['FREQ'] == 'MONTHLY') {
|
293 |
|
|
date_date_set($current_day, $year, $month, 1);
|
294 |
|
|
}
|
295 |
|
|
else {
|
296 |
|
|
date_date_set($current_day, $year, 1, 1);
|
297 |
|
|
}
|
298 |
|
|
// Jump to the next period.
|
299 |
|
|
date_modify($current_day, '+' . $jump . $modify_time);
|
300 |
|
|
}
|
301 |
|
|
}
|
302 |
|
|
|
303 |
599a39cd
|
Assos Assos
|
// For BYDAYs without parameters,like TU,TH (every Tues and Thur), we look
|
304 |
|
|
// for every one of those days during the frequency period. Iterate through
|
305 |
|
|
// periods of a WEEK, MONTH, or YEAR, checking for the days of the week
|
306 |
|
|
// that match our criteria for each week in the period, then jumping ahead
|
307 |
|
|
// to the next week, month, or year, an INTERVAL at a time.
|
308 |
b720ea3e
|
Assos Assos
|
if (!empty($week_days) &&
|
309 |
|
|
in_array($rrule['FREQ'], array('MONTHLY', 'WEEKLY', 'YEARLY'))) {
|
310 |
85ad3d82
|
Assos Assos
|
$finished = FALSE;
|
311 |
599a39cd
|
Assos Assos
|
$current_day = clone $start_date;
|
312 |
85ad3d82
|
Assos Assos
|
$format = $rrule['FREQ'] == 'YEARLY' ? 'Y' : 'n';
|
313 |
|
|
$current_period = date_format($current_day, $format);
|
314 |
|
|
|
315 |
|
|
// Back up to the beginning of the week in case we are somewhere in the
|
316 |
599a39cd
|
Assos Assos
|
// middle of the possible week days, needed so we don't prematurely jump
|
317 |
|
|
// to the next week. The date_repeat_add_dates() function will keep dates
|
318 |
|
|
// outside the range from getting added.
|
319 |
85ad3d82
|
Assos Assos
|
if (date_format($current_day, 'l') != $day_names[$day]) {
|
320 |
|
|
date_modify($current_day, '-1 ' . $week_start_day . $modify_time);
|
321 |
|
|
}
|
322 |
|
|
while (!$finished) {
|
323 |
|
|
$period_finished = FALSE;
|
324 |
|
|
while (!$period_finished) {
|
325 |
|
|
$moved = FALSE;
|
326 |
|
|
foreach ($week_days as $delta => $day) {
|
327 |
599a39cd
|
Assos Assos
|
// Find the next occurence of each day in this week, only add it if
|
328 |
|
|
// we are still in the current month or year. The
|
329 |
|
|
// date_repeat_add_dates() function is insufficient to test whether
|
330 |
|
|
// to include this date if we are using a rule like 'every other
|
331 |
|
|
// month', so we must explicitly test it here. If we're already on
|
332 |
|
|
// the right day, don't jump or we will prematurely move into the
|
333 |
|
|
// next week.
|
334 |
85ad3d82
|
Assos Assos
|
if (date_format($current_day, 'l') != $day) {
|
335 |
|
|
date_modify($current_day, '+1 ' . $day . $modify_time);
|
336 |
|
|
$moved = TRUE;
|
337 |
|
|
}
|
338 |
|
|
if ($rrule['FREQ'] == 'WEEKLY' || date_format($current_day, $format) == $current_period) {
|
339 |
|
|
date_repeat_add_dates($days, $current_day, $start_date, $end_date, $exceptions, $rrule);
|
340 |
|
|
}
|
341 |
|
|
}
|
342 |
|
|
$finished = date_repeat_is_finished($current_day, $days, $count, $end_date);
|
343 |
|
|
|
344 |
599a39cd
|
Assos Assos
|
// Make sure we don't get stuck in endless loop if the current day
|
345 |
|
|
// never got changed above.
|
346 |
85ad3d82
|
Assos Assos
|
if (!$moved) {
|
347 |
|
|
date_modify($current_day, '+1 day' . $modify_time);
|
348 |
|
|
}
|
349 |
|
|
|
350 |
599a39cd
|
Assos Assos
|
// If this is a WEEKLY frequency, stop after each week, otherwise,
|
351 |
|
|
// stop when we've moved outside the current period. Jump to the end
|
352 |
|
|
// of the week, then test the period.
|
353 |
85ad3d82
|
Assos Assos
|
if ($finished || $rrule['FREQ'] == 'WEEKLY') {
|
354 |
|
|
$period_finished = TRUE;
|
355 |
|
|
}
|
356 |
|
|
elseif ($rrule['FREQ'] != 'WEEKLY' && date_format($current_day, $format) != $current_period) {
|
357 |
|
|
$period_finished = TRUE;
|
358 |
|
|
}
|
359 |
|
|
}
|
360 |
|
|
|
361 |
|
|
if ($finished) {
|
362 |
|
|
continue;
|
363 |
|
|
}
|
364 |
|
|
|
365 |
599a39cd
|
Assos Assos
|
// We'll be at the end of a week, month, or year when we get to this
|
366 |
|
|
// point in the code. Go back to the beginning of this period before we
|
367 |
|
|
// jump, to ensure we jump to the first day of the next period.
|
368 |
85ad3d82
|
Assos Assos
|
switch ($rrule['FREQ']) {
|
369 |
|
|
case 'WEEKLY':
|
370 |
|
|
date_modify($current_day, '+1 ' . $week_start_day . $modify_time);
|
371 |
|
|
date_modify($current_day, '-1 week' . $modify_time);
|
372 |
|
|
break;
|
373 |
b720ea3e
|
Assos Assos
|
|
374 |
85ad3d82
|
Assos Assos
|
case 'MONTHLY':
|
375 |
|
|
date_modify($current_day, '-' . (date_format($current_day, 'j') - 1) . ' days' . $modify_time);
|
376 |
|
|
date_modify($current_day, '-1 month' . $modify_time);
|
377 |
|
|
break;
|
378 |
b720ea3e
|
Assos Assos
|
|
379 |
85ad3d82
|
Assos Assos
|
case 'YEARLY':
|
380 |
|
|
date_modify($current_day, '-' . date_format($current_day, 'z') . ' days' . $modify_time);
|
381 |
|
|
date_modify($current_day, '-1 year' . $modify_time);
|
382 |
|
|
break;
|
383 |
|
|
}
|
384 |
|
|
// Jump ahead to the next period to be evaluated.
|
385 |
|
|
date_modify($current_day, '+' . $jump . $modify_time);
|
386 |
|
|
$current_period = date_format($current_day, $format);
|
387 |
|
|
$finished = date_repeat_is_finished($current_day, $days, $count, $end_date);
|
388 |
|
|
}
|
389 |
|
|
}
|
390 |
|
|
}
|
391 |
|
|
|
392 |
b720ea3e
|
Assos Assos
|
// Add additional dates.
|
393 |
85ad3d82
|
Assos Assos
|
foreach ($additions as $addition) {
|
394 |
|
|
$date = new dateObject($addition . ' ' . $start_date->format('H:i:s'), $timezone);
|
395 |
|
|
$days[] = date_format($date, DATE_FORMAT_DATETIME);
|
396 |
|
|
}
|
397 |
|
|
|
398 |
|
|
sort($days);
|
399 |
|
|
return $days;
|
400 |
|
|
}
|
401 |
|
|
|
402 |
|
|
/**
|
403 |
|
|
* See if the RRULE needs some imputed values added to it.
|
404 |
|
|
*/
|
405 |
|
|
function date_repeat_adjust_rrule($rrule, $start_date) {
|
406 |
|
|
// If this is not a valid value, do nothing;
|
407 |
|
|
if (empty($rrule) || empty($rrule['FREQ'])) {
|
408 |
|
|
return array();
|
409 |
|
|
}
|
410 |
|
|
|
411 |
|
|
// RFC 2445 says if no day or monthday is specified when creating repeats for
|
412 |
|
|
// weeks, months, or years, impute the value from the start date.
|
413 |
|
|
if (empty($rrule['BYDAY']) && $rrule['FREQ'] == 'WEEKLY') {
|
414 |
|
|
$rrule['BYDAY'] = array(date_repeat_dow2day(date_format($start_date, 'w')));
|
415 |
|
|
}
|
416 |
|
|
elseif (empty($rrule['BYDAY']) && empty($rrule['BYMONTHDAY']) && $rrule['FREQ'] == 'MONTHLY') {
|
417 |
|
|
$rrule['BYMONTHDAY'] = array(date_format($start_date, 'j'));
|
418 |
|
|
}
|
419 |
|
|
elseif (empty($rrule['BYDAY']) && empty($rrule['BYMONTHDAY']) && empty($rrule['BYYEARDAY']) && $rrule['FREQ'] == 'YEARLY') {
|
420 |
|
|
$rrule['BYMONTHDAY'] = array(date_format($start_date, 'j'));
|
421 |
|
|
if (empty($rrule['BYMONTH'])) {
|
422 |
|
|
$rrule['BYMONTH'] = array(date_format($start_date, 'n'));
|
423 |
|
|
}
|
424 |
|
|
}
|
425 |
599a39cd
|
Assos Assos
|
// If we are processing rules for period other than YEARLY or MONTHLY and have
|
426 |
|
|
// BYDAYS like 2SU or -1SA, simplify them to SU or SA since the position
|
427 |
|
|
// rules make no sense in other periods and just add complexity.
|
428 |
85ad3d82
|
Assos Assos
|
elseif (!empty($rrule['BYDAY']) && !in_array($rrule['FREQ'], array('MONTHLY', 'YEARLY'))) {
|
429 |
b720ea3e
|
Assos Assos
|
foreach ($rrule['BYDAY'] as $delta => $by_day) {
|
430 |
|
|
$rrule['BYDAY'][$delta] = substr($by_day, -2);
|
431 |
85ad3d82
|
Assos Assos
|
}
|
432 |
|
|
}
|
433 |
|
|
|
434 |
|
|
return $rrule;
|
435 |
|
|
}
|
436 |
|
|
|
437 |
|
|
/**
|
438 |
|
|
* Helper function to add found date to the $dates array.
|
439 |
|
|
*
|
440 |
599a39cd
|
Assos Assos
|
* Check that the date to be added is between the start and end date and that it
|
441 |
|
|
* is not in the $exceptions, nor already in the $days array, and that it meets
|
442 |
|
|
* other criteria in the RRULE.
|
443 |
85ad3d82
|
Assos Assos
|
*/
|
444 |
|
|
function date_repeat_add_dates(&$days, $current_day, $start_date, $end_date, $exceptions, $rrule) {
|
445 |
b720ea3e
|
Assos Assos
|
if (isset($rrule['COUNT']) && count($days) >= $rrule['COUNT']) {
|
446 |
85ad3d82
|
Assos Assos
|
return FALSE;
|
447 |
|
|
}
|
448 |
|
|
$formatted = date_format($current_day, DATE_FORMAT_DATETIME);
|
449 |
|
|
if (!empty($end_date) && $formatted > date_format($end_date, DATE_FORMAT_DATETIME)) {
|
450 |
|
|
return FALSE;
|
451 |
|
|
}
|
452 |
|
|
if ($formatted < date_format($start_date, DATE_FORMAT_DATETIME)) {
|
453 |
|
|
return FALSE;
|
454 |
|
|
}
|
455 |
|
|
if (in_array(date_format($current_day, 'Y-m-d'), $exceptions)) {
|
456 |
|
|
return FALSE;
|
457 |
|
|
}
|
458 |
|
|
if (!empty($rrule['BYDAY'])) {
|
459 |
b720ea3e
|
Assos Assos
|
$by_days = $rrule['BYDAY'];
|
460 |
|
|
foreach ($by_days as $delta => $by_day) {
|
461 |
|
|
$by_days[$delta] = substr($by_day, -2);
|
462 |
85ad3d82
|
Assos Assos
|
}
|
463 |
b720ea3e
|
Assos Assos
|
if (!in_array(date_repeat_dow2day(date_format($current_day, 'w')), $by_days)) {
|
464 |
85ad3d82
|
Assos Assos
|
return FALSE;
|
465 |
b720ea3e
|
Assos Assos
|
}
|
466 |
|
|
}
|
467 |
85ad3d82
|
Assos Assos
|
if (!empty($rrule['BYYEAR']) && !in_array(date_format($current_day, 'Y'), $rrule['BYYEAR'])) {
|
468 |
|
|
return FALSE;
|
469 |
|
|
}
|
470 |
|
|
if (!empty($rrule['BYMONTH']) && !in_array(date_format($current_day, 'n'), $rrule['BYMONTH'])) {
|
471 |
|
|
return FALSE;
|
472 |
|
|
}
|
473 |
|
|
if (!empty($rrule['BYMONTHDAY'])) {
|
474 |
|
|
// Test month days, but only if there are no negative numbers.
|
475 |
|
|
$test = TRUE;
|
476 |
b720ea3e
|
Assos Assos
|
$by_month_days = array();
|
477 |
85ad3d82
|
Assos Assos
|
foreach ($rrule['BYMONTHDAY'] as $day) {
|
478 |
|
|
if ($day > 0) {
|
479 |
b720ea3e
|
Assos Assos
|
$by_month_days[] = $day;
|
480 |
85ad3d82
|
Assos Assos
|
}
|
481 |
|
|
else {
|
482 |
|
|
$test = FALSE;
|
483 |
|
|
break;
|
484 |
|
|
}
|
485 |
|
|
}
|
486 |
b720ea3e
|
Assos Assos
|
if ($test && !empty($by_month_days) && !in_array(date_format($current_day, 'j'), $by_month_days)) {
|
487 |
85ad3d82
|
Assos Assos
|
return FALSE;
|
488 |
|
|
}
|
489 |
|
|
}
|
490 |
|
|
// Don't add a day if it is already saved so we don't throw the count off.
|
491 |
|
|
if (in_array($formatted, $days)) {
|
492 |
|
|
return TRUE;
|
493 |
|
|
}
|
494 |
|
|
else {
|
495 |
|
|
$days[] = $formatted;
|
496 |
|
|
}
|
497 |
|
|
}
|
498 |
|
|
|
499 |
|
|
/**
|
500 |
|
|
* Stop when $current_day is greater than $end_date or $count is reached.
|
501 |
|
|
*/
|
502 |
|
|
function date_repeat_is_finished($current_day, $days, $count, $end_date) {
|
503 |
b720ea3e
|
Assos Assos
|
if (($count && count($days) >= $count)
|
504 |
85ad3d82
|
Assos Assos
|
|| (!empty($end_date) && date_format($current_day, 'U') > date_format($end_date, 'U'))) {
|
505 |
|
|
return TRUE;
|
506 |
|
|
}
|
507 |
|
|
else {
|
508 |
|
|
return FALSE;
|
509 |
|
|
}
|
510 |
|
|
}
|
511 |
|
|
|
512 |
|
|
/**
|
513 |
|
|
* Set a date object to a specific day of the month.
|
514 |
|
|
*
|
515 |
|
|
* Example,
|
516 |
599a39cd
|
Assos Assos
|
* date_set_month_day($date, 'Sunday', 2, '-') will reset $date to the second
|
517 |
|
|
* to last Sunday in the month. If $day is empty, will set to the number of
|
518 |
|
|
* days from the beginning or end of the month.
|
519 |
85ad3d82
|
Assos Assos
|
*/
|
520 |
b720ea3e
|
Assos Assos
|
function date_repeat_set_month_day($date_in, $day, $count = 1, $direction = '+', $timezone = 'UTC', $modify_time = '') {
|
521 |
85ad3d82
|
Assos Assos
|
if (is_object($date_in)) {
|
522 |
|
|
$current_month = date_format($date_in, 'n');
|
523 |
|
|
|
524 |
599a39cd
|
Assos Assos
|
// Reset to the start of the month. We should be able to do this with
|
525 |
|
|
// date_date_set(), but for some reason the date occasionally gets confused
|
526 |
|
|
// if run through this function multiple times. It seems to work reliably
|
527 |
|
|
// if we create a new object each time.
|
528 |
85ad3d82
|
Assos Assos
|
$datetime = date_format($date_in, DATE_FORMAT_DATETIME);
|
529 |
|
|
$datetime = substr_replace($datetime, '01', 8, 2);
|
530 |
|
|
$date = new DateObject($datetime, $timezone);
|
531 |
|
|
if ($direction == '-') {
|
532 |
|
|
// For negative search, start from the end of the month.
|
533 |
|
|
date_modify($date, '+1 month' . $modify_time);
|
534 |
|
|
}
|
535 |
|
|
else {
|
536 |
599a39cd
|
Assos Assos
|
// For positive search, back up one day to get outside the current month,
|
537 |
|
|
// so we can catch the first of the month.
|
538 |
85ad3d82
|
Assos Assos
|
date_modify($date, '-1 day' . $modify_time);
|
539 |
|
|
}
|
540 |
|
|
|
541 |
|
|
if (empty($day)) {
|
542 |
|
|
date_modify($date, $direction . $count . ' days' . $modify_time);
|
543 |
|
|
}
|
544 |
|
|
else {
|
545 |
599a39cd
|
Assos Assos
|
// Use the English text for order, like First Sunday instead of +1 Sunday
|
546 |
|
|
// to overcome PHP5 bug, (see #369020).
|
547 |
85ad3d82
|
Assos Assos
|
$order = date_order();
|
548 |
|
|
$step = $count <= 5 ? $order[$direction . $count] : $count;
|
549 |
|
|
date_modify($date, $step . ' ' . $day . $modify_time);
|
550 |
|
|
}
|
551 |
|
|
|
552 |
|
|
// If that takes us outside the current month, don't go there.
|
553 |
|
|
if (date_format($date, 'n') == $current_month) {
|
554 |
|
|
return $date;
|
555 |
|
|
}
|
556 |
|
|
}
|
557 |
|
|
return $date_in;
|
558 |
|
|
}
|
559 |
|
|
|
560 |
|
|
/**
|
561 |
|
|
* Set a date object to a specific day of the year.
|
562 |
|
|
*
|
563 |
|
|
* Example,
|
564 |
599a39cd
|
Assos Assos
|
* date_set_year_day($date, 'Sunday', 2, '-') will reset $date to the second
|
565 |
|
|
* to last Sunday in the year. If $day is empty, will set to the number of
|
566 |
|
|
* days from the beginning or end of the year.
|
567 |
85ad3d82
|
Assos Assos
|
*/
|
568 |
b720ea3e
|
Assos Assos
|
function date_repeat_set_year_day($date_in, $month, $day, $count = 1, $direction = '+', $timezone = 'UTC', $modify_time = '') {
|
569 |
85ad3d82
|
Assos Assos
|
if (is_object($date_in)) {
|
570 |
|
|
$current_year = date_format($date_in, 'Y');
|
571 |
|
|
|
572 |
599a39cd
|
Assos Assos
|
// Reset to the start of the month. See note above.
|
573 |
85ad3d82
|
Assos Assos
|
$datetime = date_format($date_in, DATE_FORMAT_DATETIME);
|
574 |
db9ffd17
|
Assos Assos
|
$month_key = isset($month) ? $month : '01';
|
575 |
|
|
$datetime = substr_replace($datetime, $month_key . '-01', 5, 5);
|
576 |
85ad3d82
|
Assos Assos
|
$date = new DateObject($datetime, $timezone);
|
577 |
db9ffd17
|
Assos Assos
|
|
578 |
|
|
if (isset($month)) {
|
579 |
|
|
if ($direction == '-') {
|
580 |
|
|
// For negative search, start from the end of the month.
|
581 |
|
|
$modifier = '+1 month';
|
582 |
|
|
}
|
583 |
|
|
else {
|
584 |
599a39cd
|
Assos Assos
|
// For positive search, back up one day to get outside the current
|
585 |
|
|
// month, so we can catch the first of the month.
|
586 |
db9ffd17
|
Assos Assos
|
$modifier = '-1 day';
|
587 |
|
|
}
|
588 |
85ad3d82
|
Assos Assos
|
}
|
589 |
|
|
else {
|
590 |
db9ffd17
|
Assos Assos
|
if ($direction == '-') {
|
591 |
|
|
// For negative search, start from the end of the year.
|
592 |
|
|
$modifier = '+1 year';
|
593 |
|
|
}
|
594 |
|
|
else {
|
595 |
599a39cd
|
Assos Assos
|
// For positive search, back up one day to get outside the current
|
596 |
|
|
// year, so we can catch the first of the year.
|
597 |
db9ffd17
|
Assos Assos
|
$modifier = '-1 day';
|
598 |
|
|
}
|
599 |
85ad3d82
|
Assos Assos
|
}
|
600 |
db9ffd17
|
Assos Assos
|
|
601 |
|
|
date_modify($date, $modifier . $modify_time);
|
602 |
|
|
|
603 |
85ad3d82
|
Assos Assos
|
if (empty($day)) {
|
604 |
|
|
date_modify($date, $direction . $count . ' days' . $modify_time);
|
605 |
|
|
}
|
606 |
|
|
else {
|
607 |
599a39cd
|
Assos Assos
|
// Use the English text for order, like First Sunday instead of +1 Sunday
|
608 |
|
|
// to overcome PHP5 bug, (see #369020).
|
609 |
85ad3d82
|
Assos Assos
|
$order = date_order();
|
610 |
|
|
$step = $count <= 5 ? $order[$direction . $count] : $count;
|
611 |
|
|
date_modify($date, $step . ' ' . $day . $modify_time);
|
612 |
|
|
}
|
613 |
|
|
|
614 |
|
|
// If that takes us outside the current year, don't go there.
|
615 |
|
|
if (date_format($date, 'Y') == $current_year) {
|
616 |
|
|
return $date;
|
617 |
|
|
}
|
618 |
|
|
}
|
619 |
|
|
return $date_in;
|
620 |
b720ea3e
|
Assos Assos
|
} |