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