8
8
We will use two kinds of functions - those that restrict the date to allowed values and those that expand allowed values
9
9
*/
10
10
11
- function add_recur ($ times ,$ freq= '' ){
11
+ function add_recur ($ times , $ freq = '' ) {
12
12
global $ recur_data ;
13
13
global $ count , $ mArray_begin , $ mArray_end , $ except_dates , $ start_date , $ start_date_unixtime ,$ end_range_unixtime ,$ until_unixtime , $ day_offset , $ current_view ;
14
14
if (!is_array ($ times )) $ times = array ($ times );
15
15
/* BYMONTH, BYWEEKNO, BYYEARDAY, BYMONTHDAY, BYDAY, BYHOUR, BYMINUTE, BYSECOND and BYSETPOS */
16
16
#dump_times($times);
17
- if ($ current_view != 'search ' ){
18
- $ times = restrict_bymonth ($ times ,$ freq );
19
- # $times = restrict_byweekno($times,$freq);
20
- $ times = restrict_byyearday ($ times ,$ freq );
21
- $ times = restrict_bymonthday ($ times ,$ freq );
22
- $ times = restrict_byday ($ times ,$ freq );
23
- if ($ start_date_unixtime > $ mArray_begin ) $ times [] = $ start_date_unixtime ;
24
- $ times = restrict_bysetpos ($ times ,$ freq ); #echo "restrict_bysetpos" ;
17
+ if ($ current_view != 'search ' ) {
18
+ $ times = restrict_bymonth ($ times , $ freq );
19
+ # $times = restrict_byweekno($times, $freq);
20
+ $ times = restrict_byyearday ($ times , $ freq );
21
+ $ times = restrict_bymonthday ($ times , $ freq );
22
+ $ times = restrict_byday ($ times , $ freq );
23
+ if ($ start_date_unixtime > $ mArray_begin ) $ times [] = $ start_date_unixtime ;
24
+ $ times = restrict_bysetpos ($ times , $ freq );
25
25
$ times = array_unique ($ times );
26
26
sort ($ times );
27
27
}
28
- $ until_date = date ("Ymd " ,$ end_range_unixtime );
29
- foreach ($ times as $ time ){
30
- #echo "time:". date("Ymd",$time)." $until_date<br>\n";
28
+ foreach ($ times as $ time ) {
31
29
if (!isset ($ time ) || $ time == '' ) continue ;
32
- $ date = date ("Ymd " ,$ time );
33
- $ time = strtotime ("$ date 12:00:00 " );
30
+
31
+ // Why are we arbitrarily setting the time to noon?
32
+ $ date = date ('Ymd ' , $ time );
33
+ $ time = strtotime ($ date . ' 12:00:00 ' );
34
+ $ until_date = date ('Ymd ' , $ until_unixtime );
35
+ $ until_unixtime = strtotime ($ until_date . ' 12:00:00 ' );
36
+
34
37
# day offset fixes shifts across day boundaries due to time diffs.
35
38
# These are already fixed for the initial instance, but need to be fixed for recurrences
36
- if (date ("Ymd " , $ time ) != $ start_date ) $ time = $ time + $ day_offset * (24 *60 *60 );
37
- if (isset ($ time )
38
- && $ time != ''
39
- && !in_array ($ time , $ recur_data )
40
- && !in_array ($ date , $ except_dates )
41
- && $ time <= $ until_unixtime
42
- && $ time >= $ start_date_unixtime
43
- && $ date <= $ until_date
44
- ){
45
- $ count --; #echo "\n.$count\n";
46
- if ($ time >= $ mArray_begin
47
- && $ time <= $ mArray_end
48
- && $ count >= 0
49
- ) $ recur_data [] = $ time ;
39
+ if (date ('Ymd ' , $ time ) != $ start_date ) $ time = $ time + ($ day_offset * 24 * 60 * 60 );
40
+ if ((isset ($ time )) &&
41
+ (!in_array ($ time , $ recur_data )) &&
42
+ (!in_array ($ date , $ except_dates )) &&
43
+ ($ time <= $ until_unixtime ) &&
44
+ ($ time >= $ start_date_unixtime ) &&
45
+ ($ date <= $ until_date )) {
46
+ $ count --;
47
+ if (($ time >= $ mArray_begin ) &&
48
+ ($ time <= $ mArray_end )
49
+ ($ count >= 0 )) {
50
+ $ recur_data [] = $ time ;
51
+ }
52
+ }
53
+ else if (in_array ($ date , $ except_dates )) {
54
+ $ count --;
50
55
}
51
56
else if (in_array ($ date , $ except_dates )) {
52
57
$ count --;
@@ -55,74 +60,81 @@ function add_recur($times,$freq=''){
55
60
#dump_times($recur_data);
56
61
return ;
57
62
}
58
- function expand_bymonth ($ time ){
63
+
64
+ function expand_bymonth ($ time ) {
59
65
global $ bymonth , $ byweekno , $ bymonthday , $ year , $ start_unixtime , $ freq_type ;
60
- if (!empty ($ byweekno )) return $ time ;
61
- if (empty ($ bymonth )) $ bymonth = array (date (" m " , $ start_unixtime ));
62
- $ d = date (" d " , $ start_unixtime );
66
+ if (!empty ($ byweekno )) return $ time ;
67
+ if (empty ($ bymonth )) $ bymonth = array (date (' m ' , $ start_unixtime ));
68
+ $ d = date (' d ' , $ start_unixtime );
63
69
if (!empty ($ bymonthday )) $ d = 1 ;
64
- foreach ($ bymonth as $ m ){
65
- $ time = mktime (12 ,0 , 0 , $ m ,$ d ,$ year ); #echo "exm:".date("Ymd",$time)."\n" ;
70
+ foreach ($ bymonth as $ m ) {
71
+ $ time = mktime (12 , 0 , 0 , $ m , $ d , $ year );
66
72
$ times [] = $ time ;
67
73
}
68
74
return $ times ;
69
75
}
70
- function expand_byweekno ($ times ){
76
+
77
+ function expand_byweekno ($ times ) {
71
78
global $ byweekno , $ year , $ freq_type , $ wkst , $ wkst3char ;
72
79
# byweekno is only used when repeat is yearly
73
80
# when it's set, the input times are irrelevant
74
81
# it also needs a byday.
75
82
if ($ freq_type != 'year ' ) return $ times ;
76
83
if (empty ($ byweekno )) return $ times ;
77
- $ total_weeks = date ("W " ,mktime (12 ,0 ,0 ,12 ,24 ,$ year ) ) +1 ;
78
- $ w1_start = strtotime ("this $ wkst3char " , mktime (12 ,0 ,0 ,1 ,1 ,$ year ) );
79
- foreach ($ byweekno as $ weekno ){
80
- if ($ weekno < 0 ) $ weekno = $ weekno + $ total_weeks ;
81
- #echo "\n $wkst3char w1st:".date("Ymd", $w1_start)." ".date("Ymd", mktime(12,0,0,1,1,$year))." weekno:$weekno";
82
- $ new_times [] = strtotime ("+ " .(($ weekno -1 )*7 )."days " ,$ w1_start );
84
+ $ total_weeks = date ('W ' , mktime (12 , 0 , 0 , 12 , 24 , $ year )) + 1 ;
85
+ $ w1_start = strtotime ('this ' . $ wkst3char , mktime (12 , 0 , 0 , 1 , 1 , $ year ));
86
+ foreach ($ byweekno as $ weekno ) {
87
+ if ($ weekno < 0 ) {
88
+ $ weekno = $ weekno + $ total_weeks ;
89
+ }
90
+ $ new_times [] = strtotime ('+ ' . (($ weekno - 1 ) * 7 ) . ' days ' , $ w1_start );
83
91
}
84
92
#dump_times($new_times);
85
93
return $ new_times ;
86
94
}
87
95
88
- function expand_byyearday ($ times ){
96
+ function expand_byyearday ($ times ) {
89
97
global $ byyearday , $ year ;
90
98
if (empty ($ byyearday )) return $ times ;
91
99
$ py = $ year -1 ;
92
100
$ ny = $ year +1 ;
93
101
$ new_times = array ();
94
- foreach ($ times as $ time ){
95
- foreach ($ byyearday as $ yearday ){
96
- if ($ yearday > 0 ){ $ day = strtotime ("+ $ yearday days Dec 31, $ py " );#echo "\n".date("Ymd",$day)." = +$yearday days Dec 31, $py";
97
- }else $ day = strtotime ("Jan 1 $ ny $ yearday days " );
98
- if (date ("Y " ,$ day == $ year )) $ new_times [] = $ day ;
102
+ foreach ($ times as $ time ){
103
+ foreach ($ byyearday as $ yearday ) {
104
+ if ($ yearday > 0 ) {
105
+ $ day = strtotime ('+ ' . $ yearday . ' days Dec 31, ' . $ py );
106
+ }
107
+ else {
108
+ $ day = strtotime ('Jan 1 ' . $ ny . ' ' . $ yearday . ' days ' );
109
+ }
110
+ if (date ('Y ' , $ day == $ year )) $ new_times [] = $ day ;
99
111
}
100
112
}
101
113
# dump_times($new_times);
102
114
return $ new_times ;
103
115
}
104
116
105
- function expand_bymonthday ($ times ){
117
+ function expand_bymonthday ($ times ) {
106
118
global $ bymonthday , $ year ;
107
119
if (empty ($ bymonthday )) return $ times ;
108
- foreach ($ times as $ time ){
109
- $ month = date ('m ' ,$ time );
110
- foreach ($ bymonthday as $ monthday ){
111
- if ($ monthday < 0 ) $ monthday = date ("t " ,$ time ) + $ monthday +1 ;
112
- $ new_times [] = mktime (12 ,0 ,0 ,$ month ,$ monthday ,$ year );
113
- #echo "monthday:$monthday\n";
120
+ foreach ($ times as $ time ) {
121
+ $ month = date ('m ' , $ time );
122
+ foreach ($ bymonthday as $ monthday ) {
123
+ if ($ monthday < 0 ) {
124
+ $ monthday = (date ('t ' , $ time ) + $ monthday + 1 );
125
+ }
126
+ $ new_times [] = mktime (12 , 0 , 0 , $ month , $ monthday , $ year );
114
127
}
115
128
}
116
129
return $ new_times ;
117
130
}
118
131
119
- function expand_byday ($ time ){
132
+ function expand_byday ($ time ) {
120
133
global $ freq_type , $ byday , $ bymonth ,$ byweekno , $ wkst3char , $ year , $ month , $ start_unixtime , $ summary ;
121
134
if (empty ($ byday )) return array ($ time );
122
135
$ times = array ();
123
- $ the_sunday = dateOfWeek (date ("Ymd " ,$ time ), $ wkst3char );
124
- #echo "<pre>$summary $freq_type ".print_r($byday,true)."$wkst3char $the_sunday</pre>";
125
- foreach ($ byday as $ key =>$ day ) {
136
+ $ the_sunday = dateOfWeek (date ('Ymd ' , $ time ), $ wkst3char );
137
+ foreach ($ byday as $ key => $ day ) {
126
138
/* set $byday_arr
127
139
[0] => byday string, e.g. 4TH
128
140
[1] => sign/modifier
@@ -131,28 +143,30 @@ function expand_byday($time){
131
143
*/
132
144
ereg ('([-\+]{0,1})?([0-9]+)?([A-Z]{2}) ' , $ day , $ byday_arr );
133
145
$ on_day = two2threeCharDays ($ byday_arr [3 ]);
134
- switch ($ freq_type ){
146
+ switch ($ freq_type ) {
135
147
case 'week ' :
136
148
#need to find the first day of the appropriate week.
137
- $ next_date_time = strtotime (" this $ on_day" , strtotime ($ the_sunday )) + (12 * 60 * 60 );
149
+ $ next_date_time = strtotime (' this ' . $ on_day, strtotime ($ the_sunday )) + (12 * 60 * 60 );
138
150
$ times [] = $ next_date_time ;
139
151
break ;
140
152
case 'month ' :
141
- $ time = mktime (12 ,0 ,0 ,$ month ,1 ,$ year );
153
+ $ time = mktime (12 , 0 , 0 , $ month , 1 , $ year );
154
+ // FIXME: Is this supposed to fall through?
142
155
case 'year ' :
143
- if (empty ($ byweekno )){
144
- $ week_arr = array (1 ,2 , 3 , 4 , 5 );
156
+ if (empty ($ byweekno )) {
157
+ $ week_arr = array (1 , 2 , 3 , 4 , 5 );
145
158
if (isset ($ byday_arr [2 ]) && $ byday_arr [2 ] !='' ) $ week_arr = array ($ byday_arr [2 ]);
146
- $ month_start = strtotime (date (" Ym01 " , $ time )) - (24 * 60 * 60 );
147
- $ month_end = strtotime (date (" Ymt " , $ time ))+ (36 * 60 * 60 );
148
- if ($ freq_type == 'year ' && empty ($ bymonth )){
149
- $ month_start = mktime (12 ,0 , 0 , 1 , 0 , $ year );
150
- $ month_end = mktime (12 ,0 , 0 , 1 , 1 , $ year+ 1 );
159
+ $ month_start = strtotime (date (' Ym01 ' , $ time )) - (24 * 60 * 60 );
160
+ $ month_end = strtotime (date (' Ymt ' , $ time )) + (36 * 60 * 60 );
161
+ if ($ freq_type == 'year ' && empty ($ bymonth )) {
162
+ $ month_start = mktime (12 , 0 , 0 , 1 , 0 , $ year );
163
+ $ month_end = mktime (12 , 0 , 0 , 1 , 1 , $ year + 1 );
151
164
}
152
- $ month_start_day = strtolower (date ("D " , $ month_start ));
153
- foreach ($ week_arr as $ week ){
154
- #echo "<pre>$summary ".$byday_arr[1].$week.$on_day." st:".date("Ymd",$month_start)." t:".date("Ymd",$time)."\n";
155
- if ($ byday_arr [1 ] == '- ' ) $ next_date_time = strtotime ($ byday_arr [1 ].$ week .$ on_day ,$ month_end );
165
+ $ month_start_day = strtolower (date ('D ' , $ month_start ));
166
+ foreach ($ week_arr as $ week ) {
167
+ if ($ byday_arr [1 ] == '- ' ) {
168
+ $ next_date_time = strtotime ($ byday_arr [1 ] . $ week . $ on_day , $ month_end );
169
+ }
156
170
else {
157
171
# we need this special offset in case the event day coincides with the month start day
158
172
# eg: month starts on a Sunday, our event is the Nth Sunday of the month... without this
@@ -161,76 +175,82 @@ function expand_byday($time){
161
175
$ next_date_time = strtotime ($ byday_arr [1 ] . $ week . $ on_day , ($ month_start + $ special_offset ));
162
176
}
163
177
# check that we're still in the same month
164
- if (date (" m " , $ next_date_time ) == date (" m " , $ time ) ) $ times [] = $ next_date_time ;
178
+ if (date (' m ' , $ next_date_time ) == date (' m ' , $ time )) $ times [] = $ next_date_time ;
165
179
}
166
- }else {
180
+ }
181
+ else {
167
182
# byweekno should act like freq_type = week
168
- $ next_date_time = strtotime (" this $ on_day" , strtotime ($ the_sunday )) + (12 * 60 * 60 );
183
+ $ next_date_time = strtotime (' this ' . $ on_day, strtotime ($ the_sunday )) + (12 * 60 * 60 );
169
184
$ times [] = $ next_date_time ;
170
185
}
171
186
break ;
172
187
default :
173
- $ month_start = strtotime (date ("Ym01 " ,$ time ));
174
- $ next_date_time = strtotime ($ byday_arr [1 ].$ byday_arr [2 ].$ on_day , $ month_start );
188
+ $ month_start = strtotime (date ('Ym01 ' , $ time ));
189
+ $ next_date_time = strtotime ($ byday_arr [1 ] . $ byday_arr [2 ] . $ on_day , $ month_start );
190
+ break ;
175
191
}
176
192
}
177
- #echo "exp byday";dump_times($times);
178
193
return $ times ;
179
194
}
180
195
181
-
182
- function restrict_bymonth ($ times ,$ freq ='' ){
196
+ function restrict_bymonth ($ times , $ freq = '' ) {
183
197
global $ bymonth , $ byyearday ;
184
198
if (empty ($ bymonth ) || !empty ($ byyearday )) return $ times ;
185
- $ new_times= array ();
186
- foreach ($ times as $ time ){
187
- if (in_array (date (" m " , $ time ), $ bymonth )) $ new_times [] = $ time ;
199
+ $ new_times = array ();
200
+ foreach ($ times as $ time ) {
201
+ if (in_array (date (' m ' , $ time ), $ bymonth )) $ new_times [] = $ time ;
188
202
}
189
203
return $ new_times ;
190
204
}
191
- function restrict_byweekno ($ times ,$ freq ='' ){
205
+
206
+ function restrict_byweekno ($ times , $ freq = '' ) {
192
207
global $ byweekno ;
193
- if (empty ($ byweekno )) return $ times ;
194
- $ new_times =array ();
195
- foreach ($ times as $ time ) if (in_array (date ("W " , $ time ), $ byweekno )) $ new_times [] = $ time ;
208
+ if (empty ($ byweekno )) return $ times ;
209
+ $ new_times = array ();
210
+ foreach ($ times as $ time ) {
211
+ if (in_array (date ('W ' , $ time ), $ byweekno )) $ new_times [] = $ time ;
212
+ }
196
213
return $ new_times ;
197
214
198
215
}
199
- function restrict_byyearday ($ times ,$ freq ='' ){
216
+
217
+ function restrict_byyearday ($ times , $ freq = '' ) {
200
218
global $ byyearday ;
201
- if (empty ($ byyearday )) return $ times ;
202
- $ new_times= array ();
203
- foreach ($ times as $ time ){
204
- foreach ($ byyearday as $ yearday ){
205
- if ($ yearday < 0 ){
206
- $ yearday = 365 + $ yearday +1 ;
207
- if (date (" L " , $ time )) $ yearday += 1 ;
219
+ if (empty ($ byyearday )) return $ times ;
220
+ $ new_times = array ();
221
+ foreach ($ times as $ time ) {
222
+ foreach ($ byyearday as $ yearday ) {
223
+ if ($ yearday < 0 ) {
224
+ $ yearday = 365 + $ yearday + 1 ;
225
+ if (date (' L ' , $ time )) $ yearday += 1 ;
208
226
}
209
227
$ yearday_arr [] = $ yearday ;
210
228
}
211
- # date(z, $time) gives 0 for Jan 1
212
- if (in_array ((date (" z " , $ time )+ 1 ), $ yearday_arr )) $ new_times [] = $ time ;
229
+ # date('z', $time) gives 0 for Jan 1
230
+ if (in_array ((date (' z ' , $ time ) + 1 ), $ yearday_arr )) $ new_times [] = $ time ;
213
231
}
214
232
return $ new_times ;
215
233
}
216
234
217
- function restrict_bymonthday ($ times ,$ freq= '' ){
235
+ function restrict_bymonthday ($ times , $ freq = '' ) {
218
236
global $ bymonthday ;
219
- if (empty ($ bymonthday )) return $ times ;
220
- $ new_times =array ();
221
- foreach ($ times as $ time ){
222
- foreach ($ bymonthday as $ monthday ){
223
- if ($ monthday < 0 ) $ monthday = date ("t " ,$ time ) + $ monthday +1 ;
237
+ if (empty ($ bymonthday )) return $ times ;
238
+ $ new_times = array ();
239
+ foreach ($ times as $ time ) {
240
+ foreach ($ bymonthday as $ monthday ) {
241
+ if ($ monthday < 0 ) {
242
+ $ monthday = date ('t ' , $ time ) + $ monthday + 1 ;
243
+ }
224
244
$ monthday_arr [] = $ monthday ;
225
245
}
226
- if (in_array (date (" j " , $ time ), $ monthday_arr )) $ new_times [] = $ time ;
246
+ if (in_array (date (' j ' , $ time ), $ monthday_arr )) $ new_times [] = $ time ;
227
247
}
228
248
return $ new_times ;
229
249
}
230
- function restrict_byday ($ times ,$ freq= '' ){
250
+ function restrict_byday ($ times , $ freq = '' ) {
231
251
global $ byday ;
232
- if (empty ($ byday )) return $ times ;
233
- foreach ($ byday as $ key=> $ day ) {
252
+ if (empty ($ byday )) return $ times ;
253
+ foreach ($ byday as $ key => $ day ) {
234
254
/* set $byday_arr
235
255
[0] => byday string, e.g. 4TH
236
256
[1] => sign/modifier
@@ -240,27 +260,31 @@ function restrict_byday($times,$freq=''){
240
260
ereg ('([-\+]{0,1})?([0-9]{1})?([A-Z]{2}) ' , $ day , $ byday_arr );
241
261
$ byday3 [] = two2threeCharDays ($ byday_arr [3 ]);
242
262
}
243
- $ new_times =array ();
244
- foreach ($ times as $ time ) if (in_array (strtolower (date ("D " , $ time )), $ byday3 )) $ new_times [] = $ time ;
263
+ $ new_times = array ();
264
+ foreach ($ times as $ time ) {
265
+ if (in_array (strtolower (date ('D ' , $ time )), $ byday3 )) $ new_times [] = $ time ;
266
+ }
245
267
return $ new_times ;
246
268
}
247
269
248
- function restrict_bysetpos ($ times ,$ freq= '' ){
270
+ function restrict_bysetpos ($ times , $ freq = '' ) {
249
271
global $ bysetpos ;
250
- if (empty ($ bysetpos )) return $ times ;
272
+ if (empty ($ bysetpos )) return $ times ;
251
273
sort ($ times );
252
- $ new_times= array ();
253
- foreach ($ bysetpos as $ setpos ){
254
- $ new_times [] = implode ('' ,array_slice ($ times , $ setpos , 1 ));
274
+ $ new_times = array ();
275
+ foreach ($ bysetpos as $ setpos ) {
276
+ $ new_times [] = implode ('' , array_slice ($ times , $ setpos , 1 ));
255
277
}
256
278
return $ new_times ;
257
279
}
258
280
259
281
# for diagnostics
260
- function dump_times ($ times ){
282
+ function dump_times ($ times ) {
261
283
global $ summary ;
262
- echo " <pre> $ summary times: " ;
284
+ echo ' <pre> ' . $ summary . ' times:' ;
263
285
#var_dump($times);
264
- foreach ($ times as $ time ) echo "\ndate: " .date ("Y-m-d H:i:s " ,$ time );
265
- echo "</pre> " ;
286
+ foreach ($ times as $ time ) {
287
+ echo "\ndate: " . date ('Y-m-d H:i:s ' , $ time );
288
+ }
289
+ echo '</pre> ' ;
266
290
}
0 commit comments