1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
4
|
|
|
* Date: 9/21/14 |
5
|
|
|
* Time: 8:18 PM |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace NilPortugues\Validator\Validation\DateTime; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class DateTimeValidation |
15
|
|
|
* @package NilPortugues\Validator\Validation\DateTimeAttribute |
16
|
|
|
*/ |
17
|
|
|
class DateTimeValidation |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Checks if a value is a a valid datetime format. |
21
|
|
|
* |
22
|
|
|
* @param string|\DateTime $value |
23
|
|
|
* |
24
|
|
|
* @return bool |
25
|
|
|
*/ |
26
|
|
|
public static function isDateTime($value) |
27
|
|
|
{ |
28
|
|
|
if ($value instanceof \DateTime) { |
29
|
|
|
return true; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$date = new \DateTime($value); |
33
|
|
|
$errors = $date->getLastErrors(); |
34
|
|
|
|
35
|
|
|
return (0 == $errors['warning_count'] && 0 == $errors['error_count']); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string|\DateTime $value |
40
|
|
|
* |
41
|
|
|
* @return \DateTime |
42
|
|
|
*/ |
43
|
|
|
private static function convertToDateTime($value) |
44
|
|
|
{ |
45
|
|
|
if ($value instanceof \DateTime) { |
46
|
|
|
return $value; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return new \DateTime($value); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Checks if a given date is happening after the given limiting date. |
54
|
|
|
* |
55
|
|
|
* @param string|\DateTime $value |
56
|
|
|
* @param string|\DateTime $limit |
57
|
|
|
* @param bool $inclusive |
58
|
|
|
* |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
public static function isAfter($value, $limit, $inclusive = false) |
62
|
|
|
{ |
63
|
|
|
$value = self::convertToDateTime($value); |
64
|
|
|
$limit = self::convertToDateTime($limit); |
65
|
|
|
|
66
|
|
|
if (false === $inclusive) { |
67
|
|
|
return \strtotime($value->format('Y-m-d H:i:s')) > \strtotime($limit->format('Y-m-d H:i:s')); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return \strtotime($value->format('Y-m-d H:i:s')) >= \strtotime($limit->format('Y-m-d H:i:s')); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Checks if a given date is happening before the given limiting date. |
75
|
|
|
* |
76
|
|
|
* @param string|\DateTime $value |
77
|
|
|
* @param string|\DateTime $limit |
78
|
|
|
* @param bool $inclusive |
79
|
|
|
* |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
public static function isBefore($value, $limit, $inclusive = false) |
83
|
|
|
{ |
84
|
|
|
$value = self::convertToDateTime($value); |
85
|
|
|
$limit = self::convertToDateTime($limit); |
86
|
|
|
|
87
|
|
|
if (false === $inclusive) { |
88
|
|
|
return \strtotime($value->format('Y-m-d H:i:s')) < \strtotime($limit->format('Y-m-d H:i:s')); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return \strtotime($value->format('Y-m-d H:i:s')) <= \strtotime($limit->format('Y-m-d H:i:s')); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Checks if a given date is in a given range of dates. |
96
|
|
|
* |
97
|
|
|
* @param string|\DateTime $value |
98
|
|
|
* @param bool $inclusive |
99
|
|
|
* @param string $minDate |
100
|
|
|
* @param string $maxDate |
101
|
|
|
* |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
|
|
public static function isBetween($value, $minDate, $maxDate, $inclusive = false) |
105
|
|
|
{ |
106
|
|
|
if (false === $inclusive) { |
107
|
|
|
return (self::isAfter($value, $minDate, false) && self::isBefore($value, $maxDate, false)); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return (self::isAfter($value, $minDate, true) && self::isBefore($value, $maxDate, true)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $value |
115
|
|
|
* |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
public static function isWeekend($value) |
119
|
|
|
{ |
120
|
|
|
$value = self::convertToDateTime($value); |
121
|
|
|
|
122
|
|
|
return '0' == $value->format('w') || '6' == $value->format('w'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* |
127
|
|
|
* @param string $value |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
|
|
public static function isWeekday($value) |
131
|
|
|
{ |
132
|
|
|
return !self::isWeekend($value); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* |
137
|
|
|
* @param string $value |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
|
|
public static function isMonday($value) |
141
|
|
|
{ |
142
|
|
|
$value = self::convertToDateTime($value); |
143
|
|
|
|
144
|
|
|
return '1' == $value->format('w'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* |
149
|
|
|
* @param string $value |
150
|
|
|
* @return bool |
151
|
|
|
*/ |
152
|
|
|
public static function isTuesday($value) |
153
|
|
|
{ |
154
|
|
|
$value = self::convertToDateTime($value); |
155
|
|
|
|
156
|
|
|
return '2' == $value->format('w'); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* |
161
|
|
|
* @param string $value |
162
|
|
|
* @return bool |
163
|
|
|
*/ |
164
|
|
|
public static function isWednesday($value) |
165
|
|
|
{ |
166
|
|
|
$value = self::convertToDateTime($value); |
167
|
|
|
|
168
|
|
|
return '3' == $value->format('w'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* |
173
|
|
|
* @param string $value |
174
|
|
|
* @return bool |
175
|
|
|
*/ |
176
|
|
|
public static function isThursday($value) |
177
|
|
|
{ |
178
|
|
|
$value = self::convertToDateTime($value); |
179
|
|
|
|
180
|
|
|
return '4' == $value->format('w'); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* |
185
|
|
|
* @param string $value |
186
|
|
|
* @return bool |
187
|
|
|
*/ |
188
|
|
|
public static function isFriday($value) |
189
|
|
|
{ |
190
|
|
|
$value = self::convertToDateTime($value); |
191
|
|
|
|
192
|
|
|
return '5' == $value->format('w'); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* |
197
|
|
|
* @param string $value |
198
|
|
|
* @return bool |
199
|
|
|
*/ |
200
|
|
|
public static function isSaturday($value) |
201
|
|
|
{ |
202
|
|
|
$value = self::convertToDateTime($value); |
203
|
|
|
|
204
|
|
|
return '6' == $value->format('w'); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* |
209
|
|
|
* @param string $value |
210
|
|
|
* @return bool |
211
|
|
|
*/ |
212
|
|
|
public static function isSunday($value) |
213
|
|
|
{ |
214
|
|
|
$value = self::convertToDateTime($value); |
215
|
|
|
|
216
|
|
|
return '0' == $value->format('w'); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* |
221
|
|
|
* @param \DateTime $value |
222
|
|
|
* @return bool |
223
|
|
|
*/ |
224
|
|
|
public static function isToday($value) |
225
|
|
|
{ |
226
|
|
|
$value = self::convertToDateTime($value); |
227
|
|
|
|
228
|
|
|
$date = new \DateTime('now'); |
229
|
|
|
|
230
|
|
|
return $date->format('Y-m-d') === $value->format('Y-m-d'); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* |
235
|
|
|
* @param \DateTime $value |
236
|
|
|
* @return bool |
237
|
|
|
*/ |
238
|
|
|
public static function isYesterday($value) |
239
|
|
|
{ |
240
|
|
|
$value = self::convertToDateTime($value); |
241
|
|
|
|
242
|
|
|
$date = new \DateTime('now - 1 day'); |
243
|
|
|
|
244
|
|
|
return $date->format('Y-m-d') === $value->format('Y-m-d'); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* |
249
|
|
|
* @param \DateTime $value |
250
|
|
|
* @return bool |
251
|
|
|
*/ |
252
|
|
|
public static function isTomorrow($value) |
253
|
|
|
{ |
254
|
|
|
$value = self::convertToDateTime($value); |
255
|
|
|
|
256
|
|
|
$date = new \DateTime('now + 1 day'); |
257
|
|
|
|
258
|
|
|
return $date->format('Y-m-d') === $value->format('Y-m-d'); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Determines if the instance is a leap year |
263
|
|
|
* |
264
|
|
|
* |
265
|
|
|
* @param \DateTime $value |
266
|
|
|
* @return bool |
267
|
|
|
*/ |
268
|
|
|
public static function isLeapYear($value) |
269
|
|
|
{ |
270
|
|
|
$value = self::convertToDateTime($value); |
271
|
|
|
|
272
|
|
|
return '1' == $value->format('L'); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* |
277
|
|
|
* @param string $value |
278
|
|
|
* @return bool |
279
|
|
|
*/ |
280
|
|
|
public static function isMorning($value) |
281
|
|
|
{ |
282
|
|
|
$value = self::convertToDateTime($value); |
283
|
|
|
$date = \strtotime($value->format('H:i:s')); |
284
|
|
|
|
285
|
|
|
return $date >= \strtotime($value->format('06:00:00')) && $date <= \strtotime($value->format('11:59:59')); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* |
290
|
|
|
* @param string $value |
291
|
|
|
* @return bool |
292
|
|
|
*/ |
293
|
|
|
public static function isAftenoon($value) |
294
|
|
|
{ |
295
|
|
|
$value = self::convertToDateTime($value); |
296
|
|
|
$date = \strtotime($value->format('H:i:s')); |
297
|
|
|
|
298
|
|
|
return $date >= \strtotime($value->format('12:00:00')) && $date <= \strtotime($value->format('17:59:59')); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* |
303
|
|
|
* @param string $value |
304
|
|
|
* @return bool |
305
|
|
|
*/ |
306
|
|
|
public static function isEvening($value) |
307
|
|
|
{ |
308
|
|
|
$value = self::convertToDateTime($value); |
309
|
|
|
$date = \strtotime($value->format('H:i:s')); |
310
|
|
|
|
311
|
|
|
return $date >= \strtotime($value->format('18:00:00')) && $date <= \strtotime($value->format('23:59:59')); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* |
316
|
|
|
* @param string $value |
317
|
|
|
* @return bool |
318
|
|
|
*/ |
319
|
|
|
public static function isNight($value) |
320
|
|
|
{ |
321
|
|
|
$value = self::convertToDateTime($value); |
322
|
|
|
$date = \strtotime($value->format('H:i:s')); |
323
|
|
|
|
324
|
|
|
return $date >= \strtotime($value->format('00:00:00')) && $date <= \strtotime($value->format('05:59:59')); |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|