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