1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DavideCasiraghi\LaravelEventsCalendar; |
4
|
|
|
|
5
|
|
|
class LaravelEventsCalendar |
6
|
|
|
{ |
7
|
|
|
/***************************************************************************/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Format a date from datepicker (d/m/Y) to a format ready to be stored on DB (Y-m-d). |
11
|
|
|
* If the date picker date is null return today's date. |
12
|
|
|
* the PARAM is a date in the d/m/Y format - the RETURN is a date in the Y-m-d format. |
13
|
|
|
* If $todaysDateIfNull = 1, when the date is null return the date of today. |
14
|
|
|
* |
15
|
|
|
* @param string $DatePickerDate |
16
|
|
|
* @param bool $todaysDateIfNull |
17
|
|
|
* @return string $ret |
18
|
|
|
*/ |
19
|
1 |
|
public static function formatDatePickerDateForMysql($DatePickerDate, $todaysDateIfNull = 0) |
20
|
|
|
{ |
21
|
1 |
|
if ($DatePickerDate) { |
22
|
1 |
|
[$tid, $tim, $tiy] = explode('/', $DatePickerDate); |
23
|
1 |
|
$ret = "$tiy-$tim-$tid"; |
24
|
1 |
|
} elseif ($todaysDateIfNull) { |
25
|
1 |
|
date_default_timezone_set('Europe/Rome'); |
26
|
1 |
|
$ret = date('Y-m-d', time()); |
27
|
|
|
} else { |
28
|
1 |
|
$ret = null; |
29
|
|
|
} |
30
|
|
|
|
31
|
1 |
|
return $ret; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/***************************************************************************/ |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* It returns a string that is composed by the array values separated by a comma. |
38
|
|
|
* |
39
|
|
|
* @param array $array |
40
|
|
|
* @return string $ret |
41
|
|
|
*/ |
42
|
3 |
|
public function getStringFromArraySeparatedByComma($array) |
43
|
|
|
{ |
44
|
3 |
|
$ret = ''; |
45
|
3 |
|
$i = 0; |
46
|
3 |
|
$len = count($array); // to put "," to all items except the last |
47
|
|
|
|
48
|
3 |
|
foreach ($array as $key => $value) { |
49
|
3 |
|
$ret .= $value; |
50
|
3 |
|
if ($i != $len - 1) { // not last |
51
|
1 |
|
$ret .= ', '; |
52
|
|
|
} |
53
|
3 |
|
$i++; |
54
|
|
|
} |
55
|
|
|
|
56
|
3 |
|
return $ret; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/***************************************************************************/ |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Check the date and return true if the weekday is the one specified in $dayOfTheWeek. eg. if $dayOfTheWeek = 3, is true if the date is a Wednesday |
64
|
|
|
* $dayOfTheWeek: 1|2|3|4|5|6|7 (MONDAY-SUNDAY) |
65
|
|
|
* https://stackoverflow.com/questions/2045736/getting-all-dates-for-mondays-and-tuesdays-for-the-next-year. |
66
|
|
|
* |
67
|
|
|
* @param \DavideCasiraghi\LaravelEventsCalendar\Models\Event $event |
68
|
|
|
* @param string $date |
69
|
|
|
* @param int $dayOfTheWeek |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
3 |
|
public function isWeekDay($date, $dayOfTheWeek) |
73
|
|
|
{ |
74
|
|
|
// Fix the bug that was avoiding to save Sunday. Date 'w' identify sunday as 0 and not 7. |
75
|
3 |
|
if ($dayOfTheWeek == 7) { |
76
|
1 |
|
$dayOfTheWeek = 0; |
77
|
|
|
} |
78
|
|
|
|
79
|
3 |
|
return date('w', strtotime($date)) == $dayOfTheWeek; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/***************************************************************************/ |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* GET number of the specified weekday in this month (1 for the first). |
86
|
|
|
* $dateTimestamp - unix timestramp of the date specified |
87
|
|
|
* $dayOfWeekValue - 1 (for Monday) through 7 (for Sunday) |
88
|
|
|
* Return the number of the week in the month of the weekday specified. |
89
|
|
|
* @param string $dateTimestamp |
90
|
|
|
* @param string $dayOfWeekValue |
91
|
|
|
* @return int |
92
|
|
|
*/ |
93
|
2 |
|
public function weekdayNumberOfMonth($dateTimestamp, $dayOfWeekValue) |
94
|
|
|
{ |
95
|
2 |
|
$cut = substr($dateTimestamp, 0, 8); |
96
|
2 |
|
$daylen = 86400; |
97
|
2 |
|
$timestamp = strtotime($dateTimestamp); |
98
|
2 |
|
$first = strtotime($cut.'01'); |
99
|
2 |
|
$elapsed = (($timestamp - $first) / $daylen) + 1; |
100
|
2 |
|
$i = 1; |
101
|
2 |
|
$weeks = 0; |
102
|
2 |
|
for ($i == 1; $i <= $elapsed; $i++) { |
103
|
2 |
|
$dayfind = $cut.(strlen($i) < 2 ? '0'.$i : $i); |
104
|
2 |
|
$daytimestamp = strtotime($dayfind); |
105
|
2 |
|
$day = strtolower(date('N', $daytimestamp)); |
106
|
2 |
|
if ($day == strtolower($dayOfWeekValue)) { |
107
|
1 |
|
$weeks++; |
108
|
|
|
} |
109
|
|
|
} |
110
|
2 |
|
if ($weeks == 0) { |
|
|
|
|
111
|
1 |
|
$weeks++; |
112
|
|
|
} |
113
|
|
|
|
114
|
2 |
|
return $weeks; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/***************************************************************************/ |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* GET number of week from the end of the month - https://stackoverflow.com/questions/5853380/php-get-number-of-week-for-month |
121
|
|
|
* Week of the month = Week of the year - Week of the year of first day of month + 1. |
122
|
|
|
* Return the number of the week in the month of the day specified |
123
|
|
|
* $when - unix timestramp of the date specified. |
124
|
|
|
* |
125
|
|
|
* @param string $when |
126
|
|
|
* @return int |
127
|
|
|
*/ |
128
|
2 |
|
public function weekOfMonthFromTheEnd($when = null) |
129
|
|
|
{ |
130
|
2 |
|
$numberOfDayOfTheMonth = strftime('%e', $when); // Day of the month 1-31 |
|
|
|
|
131
|
2 |
|
$lastDayOfMonth = strftime('%e', strtotime(date('Y-m-t', $when))); // the last day of the month of the specified date |
|
|
|
|
132
|
2 |
|
$dayDifference = $lastDayOfMonth - $numberOfDayOfTheMonth; |
133
|
|
|
|
134
|
|
|
switch (true) { |
135
|
2 |
|
case $dayDifference < 7: |
136
|
|
|
$weekFromTheEnd = 1; |
137
|
|
|
break; |
138
|
|
|
|
139
|
2 |
|
case $dayDifference < 14: |
140
|
|
|
$weekFromTheEnd = 2; |
141
|
|
|
break; |
142
|
|
|
|
143
|
2 |
|
case $dayDifference < 21: |
144
|
1 |
|
$weekFromTheEnd = 3; |
145
|
1 |
|
break; |
146
|
|
|
|
147
|
1 |
|
case $dayDifference < 28: |
148
|
1 |
|
$weekFromTheEnd = 4; |
149
|
1 |
|
break; |
150
|
|
|
|
151
|
|
|
default: |
152
|
|
|
$weekFromTheEnd = 5; |
153
|
|
|
break; |
154
|
|
|
} |
155
|
|
|
|
156
|
2 |
|
return $weekFromTheEnd; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/***************************************************************************/ |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* GET number of day from the end of the month. |
163
|
|
|
* $when - unix timestramp of the date specified |
164
|
|
|
* Return the number of day of the month from end. |
165
|
|
|
* |
166
|
|
|
* @param string $when |
167
|
|
|
* @return int |
168
|
|
|
*/ |
169
|
2 |
|
public function dayOfMonthFromTheEnd($when = null) |
170
|
|
|
{ |
171
|
2 |
|
$numberOfDayOfTheMonth = strftime('%e', $when); // Day of the month 1-31 |
|
|
|
|
172
|
2 |
|
$lastDayOfMonth = strftime('%e', strtotime(date('Y-m-t', $when))); // the last day of the month of the specified date |
|
|
|
|
173
|
2 |
|
$dayDifference = $lastDayOfMonth - $numberOfDayOfTheMonth; |
174
|
|
|
|
175
|
2 |
|
return $dayDifference; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/***************************************************************************/ |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* GET the ordinal indicator - for the day of the month. |
182
|
|
|
* Return the ordinal indicator (st, nd, rd, th). |
183
|
|
|
* @param int $number |
184
|
|
|
* @return string |
185
|
|
|
*/ |
186
|
4 |
|
public function getOrdinalIndicator($number) |
187
|
|
|
{ |
188
|
|
|
switch ($number) { |
189
|
4 |
|
case 1: |
190
|
1 |
|
$ret = 'st'; |
191
|
1 |
|
break; |
192
|
4 |
|
case 2: |
193
|
2 |
|
$ret = 'nd'; |
194
|
2 |
|
break; |
195
|
4 |
|
case 3: |
196
|
1 |
|
$ret = 'rd'; |
197
|
1 |
|
break; |
198
|
|
|
default: |
199
|
4 |
|
$ret = 'th'; |
200
|
4 |
|
break; |
201
|
|
|
} |
202
|
|
|
|
203
|
4 |
|
return $ret; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/***************************************************************************/ |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Decode the event repeat_weekly_on field - used in event.show. |
210
|
|
|
* Return a string like "Monday". |
211
|
|
|
* |
212
|
|
|
* @param string $repeatWeeklyOn |
213
|
|
|
* @return string |
214
|
|
|
*/ |
215
|
2 |
|
public function decodeRepeatWeeklyOn($repeatWeeklyOn) |
216
|
|
|
{ |
217
|
2 |
|
$weekdayArray = ['', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']; |
218
|
2 |
|
$ret = $weekdayArray[$repeatWeeklyOn]; |
219
|
|
|
|
220
|
2 |
|
return $ret; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/***************************************************************************/ |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Decode the event on_monthly_kind field - used in event.show. |
227
|
|
|
* Return a string like "the 4th to last Thursday of the month". |
228
|
|
|
* |
229
|
|
|
* @param string $onMonthlyKindCode |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
2 |
|
public function decodeOnMonthlyKind($onMonthlyKindCode) |
233
|
|
|
{ |
234
|
2 |
|
$onMonthlyKindCodeArray = explode('|', $onMonthlyKindCode); |
235
|
2 |
|
$weekDays = ['', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']; |
236
|
|
|
|
237
|
|
|
//dd($onMonthlyKindCodeArray); |
238
|
2 |
|
switch ($onMonthlyKindCodeArray[0]) { |
239
|
2 |
|
case '0': // 0|7 eg. the 7th day of the month |
240
|
2 |
|
$dayNumber = $onMonthlyKindCodeArray[1]; |
241
|
2 |
|
$ordinalIndicator = LaravelEventsCalendar::getOrdinalIndicator($dayNumber); |
|
|
|
|
242
|
|
|
|
243
|
2 |
|
$dayNumberOrdinal = $dayNumber.$ordinalIndicator; |
244
|
2 |
|
$format = 'the %s day of the month'; |
245
|
2 |
|
$ret = sprintf($format, $dayNumberOrdinal); |
246
|
2 |
|
break; |
247
|
1 |
|
case '1': // 1|2|4 eg. the 2nd Thursday of the month |
248
|
1 |
|
$dayNumber = $onMonthlyKindCodeArray[1]; |
249
|
1 |
|
$ordinalIndicator = LaravelEventsCalendar::getOrdinalIndicator($dayNumber); |
250
|
|
|
|
251
|
1 |
|
$dayNumberOrdinal = $dayNumber.$ordinalIndicator; |
252
|
1 |
|
$weekDay = $weekDays[$onMonthlyKindCodeArray[2]]; // Monday, Tuesday, Wednesday |
253
|
1 |
|
$format = 'the %s %s of the month'; |
254
|
1 |
|
$ret = sprintf($format, $dayNumberOrdinal, $weekDay); |
255
|
1 |
|
break; |
256
|
1 |
|
case '2': // 2|20 eg. the 21th to last day of the month |
257
|
1 |
|
$dayNumber = $onMonthlyKindCodeArray[1] + 1; |
258
|
1 |
|
$ordinalIndicator = LaravelEventsCalendar::getOrdinalIndicator($dayNumber); |
259
|
|
|
|
260
|
1 |
|
$dayNumberOrdinal = $dayNumber.$ordinalIndicator; |
261
|
1 |
|
$format = 'the %s to last day of the month'; |
262
|
1 |
|
$ret = sprintf($format, $dayNumberOrdinal); |
263
|
1 |
|
break; |
264
|
1 |
|
case '3': // 3|3|4 eg. the 4th to last Thursday of the month |
265
|
1 |
|
$dayNumber = $onMonthlyKindCodeArray[1] + 1; |
266
|
1 |
|
$ordinalIndicator = LaravelEventsCalendar::getOrdinalIndicator($dayNumber); |
267
|
|
|
|
268
|
1 |
|
$dayNumberOrdinal = $dayNumber.$ordinalIndicator; |
269
|
1 |
|
$weekDay = $weekDays[$onMonthlyKindCodeArray[2]]; // Monday, Tuesday, Wednesday |
270
|
1 |
|
$format = 'the %s to last %s of the month'; |
271
|
1 |
|
$ret = sprintf($format, $dayNumberOrdinal, $weekDay); |
272
|
1 |
|
break; |
273
|
|
|
} |
274
|
|
|
|
275
|
2 |
|
return $ret; |
|
|
|
|
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
} |
279
|
|
|
|