Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/**
4
 * This file is part of the Carbon package.
5
 *
6
 * (c) Brian Nesbitt <brian@nesbot.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Carbon\Traits;
13
 
14
use Carbon\Exceptions\UnknownUnitException;
15
 
16
/**
17
 * Trait Boundaries.
18
 *
19
 * startOf, endOf and derived method for each unit.
20
 *
21
 * Depends on the following properties:
22
 *
23
 * @property int $year
24
 * @property int $month
25
 * @property int $daysInMonth
26
 * @property int $quarter
27
 *
28
 * Depends on the following methods:
29
 *
30
 * @method $this setTime(int $hour, int $minute, int $second = 0, int $microseconds = 0)
31
 * @method $this setDate(int $year, int $month, int $day)
32
 * @method $this addMonths(int $value = 1)
33
 */
34
trait Boundaries
35
{
36
    /**
37
     * Resets the time to 00:00:00 start of day
38
     *
39
     * @example
40
     * ```
41
     * echo Carbon::parse('2018-07-25 12:45:16')->startOfDay();
42
     * ```
43
     *
44
     * @return static
45
     */
46
    public function startOfDay()
47
    {
48
        return $this->setTime(0, 0, 0, 0);
49
    }
50
 
51
    /**
52
     * Resets the time to 23:59:59.999999 end of day
53
     *
54
     * @example
55
     * ```
56
     * echo Carbon::parse('2018-07-25 12:45:16')->endOfDay();
57
     * ```
58
     *
59
     * @return static
60
     */
61
    public function endOfDay()
62
    {
63
        return $this->setTime(static::HOURS_PER_DAY - 1, static::MINUTES_PER_HOUR - 1, static::SECONDS_PER_MINUTE - 1, static::MICROSECONDS_PER_SECOND - 1);
64
    }
65
 
66
    /**
67
     * Resets the date to the first day of the month and the time to 00:00:00
68
     *
69
     * @example
70
     * ```
71
     * echo Carbon::parse('2018-07-25 12:45:16')->startOfMonth();
72
     * ```
73
     *
74
     * @return static
75
     */
76
    public function startOfMonth()
77
    {
78
        return $this->setDate($this->year, $this->month, 1)->startOfDay();
79
    }
80
 
81
    /**
82
     * Resets the date to end of the month and time to 23:59:59.999999
83
     *
84
     * @example
85
     * ```
86
     * echo Carbon::parse('2018-07-25 12:45:16')->endOfMonth();
87
     * ```
88
     *
89
     * @return static
90
     */
91
    public function endOfMonth()
92
    {
93
        return $this->setDate($this->year, $this->month, $this->daysInMonth)->endOfDay();
94
    }
95
 
96
    /**
97
     * Resets the date to the first day of the quarter and the time to 00:00:00
98
     *
99
     * @example
100
     * ```
101
     * echo Carbon::parse('2018-07-25 12:45:16')->startOfQuarter();
102
     * ```
103
     *
104
     * @return static
105
     */
106
    public function startOfQuarter()
107
    {
108
        $month = ($this->quarter - 1) * static::MONTHS_PER_QUARTER + 1;
109
 
110
        return $this->setDate($this->year, $month, 1)->startOfDay();
111
    }
112
 
113
    /**
114
     * Resets the date to end of the quarter and time to 23:59:59.999999
115
     *
116
     * @example
117
     * ```
118
     * echo Carbon::parse('2018-07-25 12:45:16')->endOfQuarter();
119
     * ```
120
     *
121
     * @return static
122
     */
123
    public function endOfQuarter()
124
    {
125
        return $this->startOfQuarter()->addMonths(static::MONTHS_PER_QUARTER - 1)->endOfMonth();
126
    }
127
 
128
    /**
129
     * Resets the date to the first day of the year and the time to 00:00:00
130
     *
131
     * @example
132
     * ```
133
     * echo Carbon::parse('2018-07-25 12:45:16')->startOfYear();
134
     * ```
135
     *
136
     * @return static
137
     */
138
    public function startOfYear()
139
    {
140
        return $this->setDate($this->year, 1, 1)->startOfDay();
141
    }
142
 
143
    /**
144
     * Resets the date to end of the year and time to 23:59:59.999999
145
     *
146
     * @example
147
     * ```
148
     * echo Carbon::parse('2018-07-25 12:45:16')->endOfYear();
149
     * ```
150
     *
151
     * @return static
152
     */
153
    public function endOfYear()
154
    {
155
        return $this->setDate($this->year, 12, 31)->endOfDay();
156
    }
157
 
158
    /**
159
     * Resets the date to the first day of the decade and the time to 00:00:00
160
     *
161
     * @example
162
     * ```
163
     * echo Carbon::parse('2018-07-25 12:45:16')->startOfDecade();
164
     * ```
165
     *
166
     * @return static
167
     */
168
    public function startOfDecade()
169
    {
170
        $year = $this->year - $this->year % static::YEARS_PER_DECADE;
171
 
172
        return $this->setDate($year, 1, 1)->startOfDay();
173
    }
174
 
175
    /**
176
     * Resets the date to end of the decade and time to 23:59:59.999999
177
     *
178
     * @example
179
     * ```
180
     * echo Carbon::parse('2018-07-25 12:45:16')->endOfDecade();
181
     * ```
182
     *
183
     * @return static
184
     */
185
    public function endOfDecade()
186
    {
187
        $year = $this->year - $this->year % static::YEARS_PER_DECADE + static::YEARS_PER_DECADE - 1;
188
 
189
        return $this->setDate($year, 12, 31)->endOfDay();
190
    }
191
 
192
    /**
193
     * Resets the date to the first day of the century and the time to 00:00:00
194
     *
195
     * @example
196
     * ```
197
     * echo Carbon::parse('2018-07-25 12:45:16')->startOfCentury();
198
     * ```
199
     *
200
     * @return static
201
     */
202
    public function startOfCentury()
203
    {
204
        $year = $this->year - ($this->year - 1) % static::YEARS_PER_CENTURY;
205
 
206
        return $this->setDate($year, 1, 1)->startOfDay();
207
    }
208
 
209
    /**
210
     * Resets the date to end of the century and time to 23:59:59.999999
211
     *
212
     * @example
213
     * ```
214
     * echo Carbon::parse('2018-07-25 12:45:16')->endOfCentury();
215
     * ```
216
     *
217
     * @return static
218
     */
219
    public function endOfCentury()
220
    {
221
        $year = $this->year - 1 - ($this->year - 1) % static::YEARS_PER_CENTURY + static::YEARS_PER_CENTURY;
222
 
223
        return $this->setDate($year, 12, 31)->endOfDay();
224
    }
225
 
226
    /**
227
     * Resets the date to the first day of the millennium and the time to 00:00:00
228
     *
229
     * @example
230
     * ```
231
     * echo Carbon::parse('2018-07-25 12:45:16')->startOfMillennium();
232
     * ```
233
     *
234
     * @return static
235
     */
236
    public function startOfMillennium()
237
    {
238
        $year = $this->year - ($this->year - 1) % static::YEARS_PER_MILLENNIUM;
239
 
240
        return $this->setDate($year, 1, 1)->startOfDay();
241
    }
242
 
243
    /**
244
     * Resets the date to end of the millennium and time to 23:59:59.999999
245
     *
246
     * @example
247
     * ```
248
     * echo Carbon::parse('2018-07-25 12:45:16')->endOfMillennium();
249
     * ```
250
     *
251
     * @return static
252
     */
253
    public function endOfMillennium()
254
    {
255
        $year = $this->year - 1 - ($this->year - 1) % static::YEARS_PER_MILLENNIUM + static::YEARS_PER_MILLENNIUM;
256
 
257
        return $this->setDate($year, 12, 31)->endOfDay();
258
    }
259
 
260
    /**
261
     * Resets the date to the first day of week (defined in $weekStartsAt) and the time to 00:00:00
262
     *
263
     * @example
264
     * ```
265
     * echo Carbon::parse('2018-07-25 12:45:16')->startOfWeek() . "\n";
266
     * echo Carbon::parse('2018-07-25 12:45:16')->locale('ar')->startOfWeek() . "\n";
267
     * echo Carbon::parse('2018-07-25 12:45:16')->startOfWeek(Carbon::SUNDAY) . "\n";
268
     * ```
269
     *
270
     * @param int $weekStartsAt optional start allow you to specify the day of week to use to start the week
271
     *
272
     * @return static
273
     */
274
    public function startOfWeek($weekStartsAt = null)
275
    {
276
        return $this->subDays((7 + $this->dayOfWeek - ($weekStartsAt ?? $this->firstWeekDay)) % 7)->startOfDay();
277
    }
278
 
279
    /**
280
     * Resets the date to end of week (defined in $weekEndsAt) and time to 23:59:59.999999
281
     *
282
     * @example
283
     * ```
284
     * echo Carbon::parse('2018-07-25 12:45:16')->endOfWeek() . "\n";
285
     * echo Carbon::parse('2018-07-25 12:45:16')->locale('ar')->endOfWeek() . "\n";
286
     * echo Carbon::parse('2018-07-25 12:45:16')->endOfWeek(Carbon::SATURDAY) . "\n";
287
     * ```
288
     *
289
     * @param int $weekEndsAt optional start allow you to specify the day of week to use to end the week
290
     *
291
     * @return static
292
     */
293
    public function endOfWeek($weekEndsAt = null)
294
    {
295
        return $this->addDays((7 - $this->dayOfWeek + ($weekEndsAt ?? $this->lastWeekDay)) % 7)->endOfDay();
296
    }
297
 
298
    /**
299
     * Modify to start of current hour, minutes and seconds become 0
300
     *
301
     * @example
302
     * ```
303
     * echo Carbon::parse('2018-07-25 12:45:16')->startOfHour();
304
     * ```
305
     *
306
     * @return static
307
     */
308
    public function startOfHour()
309
    {
310
        return $this->setTime($this->hour, 0, 0, 0);
311
    }
312
 
313
    /**
314
     * Modify to end of current hour, minutes and seconds become 59
315
     *
316
     * @example
317
     * ```
318
     * echo Carbon::parse('2018-07-25 12:45:16')->endOfHour();
319
     * ```
320
     *
321
     * @return static
322
     */
323
    public function endOfHour()
324
    {
325
        return $this->setTime($this->hour, static::MINUTES_PER_HOUR - 1, static::SECONDS_PER_MINUTE - 1, static::MICROSECONDS_PER_SECOND - 1);
326
    }
327
 
328
    /**
329
     * Modify to start of current minute, seconds become 0
330
     *
331
     * @example
332
     * ```
333
     * echo Carbon::parse('2018-07-25 12:45:16')->startOfMinute();
334
     * ```
335
     *
336
     * @return static
337
     */
338
    public function startOfMinute()
339
    {
340
        return $this->setTime($this->hour, $this->minute, 0, 0);
341
    }
342
 
343
    /**
344
     * Modify to end of current minute, seconds become 59
345
     *
346
     * @example
347
     * ```
348
     * echo Carbon::parse('2018-07-25 12:45:16')->endOfMinute();
349
     * ```
350
     *
351
     * @return static
352
     */
353
    public function endOfMinute()
354
    {
355
        return $this->setTime($this->hour, $this->minute, static::SECONDS_PER_MINUTE - 1, static::MICROSECONDS_PER_SECOND - 1);
356
    }
357
 
358
    /**
359
     * Modify to start of current second, microseconds become 0
360
     *
361
     * @example
362
     * ```
363
     * echo Carbon::parse('2018-07-25 12:45:16.334455')
364
     *   ->startOfSecond()
365
     *   ->format('H:i:s.u');
366
     * ```
367
     *
368
     * @return static
369
     */
370
    public function startOfSecond()
371
    {
372
        return $this->setTime($this->hour, $this->minute, $this->second, 0);
373
    }
374
 
375
    /**
376
     * Modify to end of current second, microseconds become 999999
377
     *
378
     * @example
379
     * ```
380
     * echo Carbon::parse('2018-07-25 12:45:16.334455')
381
     *   ->endOfSecond()
382
     *   ->format('H:i:s.u');
383
     * ```
384
     *
385
     * @return static
386
     */
387
    public function endOfSecond()
388
    {
389
        return $this->setTime($this->hour, $this->minute, $this->second, static::MICROSECONDS_PER_SECOND - 1);
390
    }
391
 
392
    /**
393
     * Modify to start of current given unit.
394
     *
395
     * @example
396
     * ```
397
     * echo Carbon::parse('2018-07-25 12:45:16.334455')
398
     *   ->startOf('month')
399
     *   ->endOf('week', Carbon::FRIDAY);
400
     * ```
401
     *
402
     * @param string            $unit
403
     * @param array<int, mixed> $params
404
     *
405
     * @return static
406
     */
407
    public function startOf($unit, ...$params)
408
    {
409
        $ucfUnit = ucfirst(static::singularUnit($unit));
410
        $method = "startOf$ucfUnit";
411
        if (!method_exists($this, $method)) {
412
            throw new UnknownUnitException($unit);
413
        }
414
 
415
        return $this->$method(...$params);
416
    }
417
 
418
    /**
419
     * Modify to end of current given unit.
420
     *
421
     * @example
422
     * ```
423
     * echo Carbon::parse('2018-07-25 12:45:16.334455')
424
     *   ->startOf('month')
425
     *   ->endOf('week', Carbon::FRIDAY);
426
     * ```
427
     *
428
     * @param string            $unit
429
     * @param array<int, mixed> $params
430
     *
431
     * @return static
432
     */
433
    public function endOf($unit, ...$params)
434
    {
435
        $ucfUnit = ucfirst(static::singularUnit($unit));
436
        $method = "endOf$ucfUnit";
437
        if (!method_exists($this, $method)) {
438
            throw new UnknownUnitException($unit);
439
        }
440
 
441
        return $this->$method(...$params);
442
    }
443
}