Boundaries.php
2 years ago
Cast.php
2 years ago
Comparison.php
1 year ago
Converter.php
2 years ago
Creator.php
2 years ago
Date.php
1 month ago
DeprecatedProperties.php
2 years ago
Difference.php
2 years ago
IntervalRounding.php
2 years ago
IntervalStep.php
2 years ago
Localization.php
1 month ago
Macro.php
2 years ago
MagicParameter.php
2 years ago
Mixin.php
2 years ago
Modifiers.php
2 years ago
Mutability.php
2 years ago
ObjectInitialisation.php
2 years ago
Options.php
1 year ago
Rounding.php
2 years ago
Serialization.php
2 years ago
Test.php
1 month ago
Timestamp.php
1 month ago
ToStringFormat.php
2 years ago
Units.php
2 years ago
Week.php
2 years ago
Week.php
187 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 IAWPSCOPED\Carbon\Traits; |
| 12 | |
| 13 | /** |
| 14 | * Trait Week. |
| 15 | * |
| 16 | * week and ISO week number, year and count in year. |
| 17 | * |
| 18 | * Depends on the following properties: |
| 19 | * |
| 20 | * @property int $daysInYear |
| 21 | * @property int $dayOfWeek |
| 22 | * @property int $dayOfYear |
| 23 | * @property int $year |
| 24 | * |
| 25 | * Depends on the following methods: |
| 26 | * |
| 27 | * @method static addWeeks(int $weeks = 1) |
| 28 | * @method static copy() |
| 29 | * @method static dayOfYear(int $dayOfYear) |
| 30 | * @method string getTranslationMessage(string $key, ?string $locale = null, ?string $default = null, $translator = null) |
| 31 | * @method static next(int|string $day = null) |
| 32 | * @method static startOfWeek(int $day = 1) |
| 33 | * @method static subWeeks(int $weeks = 1) |
| 34 | * @method static year(int $year = null) |
| 35 | * @internal |
| 36 | */ |
| 37 | trait Week |
| 38 | { |
| 39 | /** |
| 40 | * Set/get the week number of year using given first day of week and first |
| 41 | * day of year included in the first week. Or use ISO format if no settings |
| 42 | * given. |
| 43 | * |
| 44 | * @param int|null $year if null, act as a getter, if not null, set the year and return current instance. |
| 45 | * @param int|null $dayOfWeek first date of week from 0 (Sunday) to 6 (Saturday) |
| 46 | * @param int|null $dayOfYear first day of year included in the week #1 |
| 47 | * |
| 48 | * @return int|static |
| 49 | */ |
| 50 | public function isoWeekYear($year = null, $dayOfWeek = null, $dayOfYear = null) |
| 51 | { |
| 52 | return $this->weekYear($year, $dayOfWeek ?? 1, $dayOfYear ?? 4); |
| 53 | } |
| 54 | /** |
| 55 | * Set/get the week number of year using given first day of week and first |
| 56 | * day of year included in the first week. Or use US format if no settings |
| 57 | * given (Sunday / Jan 6). |
| 58 | * |
| 59 | * @param int|null $year if null, act as a getter, if not null, set the year and return current instance. |
| 60 | * @param int|null $dayOfWeek first date of week from 0 (Sunday) to 6 (Saturday) |
| 61 | * @param int|null $dayOfYear first day of year included in the week #1 |
| 62 | * |
| 63 | * @return int|static |
| 64 | */ |
| 65 | public function weekYear($year = null, $dayOfWeek = null, $dayOfYear = null) |
| 66 | { |
| 67 | $dayOfWeek = $dayOfWeek ?? $this->getTranslationMessage('first_day_of_week') ?? 0; |
| 68 | $dayOfYear = $dayOfYear ?? $this->getTranslationMessage('day_of_first_week_of_year') ?? 1; |
| 69 | if ($year !== null) { |
| 70 | $year = (int) \round($year); |
| 71 | if ($this->weekYear(null, $dayOfWeek, $dayOfYear) === $year) { |
| 72 | return $this->avoidMutation(); |
| 73 | } |
| 74 | $week = $this->week(null, $dayOfWeek, $dayOfYear); |
| 75 | $day = $this->dayOfWeek; |
| 76 | $date = $this->year($year); |
| 77 | switch ($date->weekYear(null, $dayOfWeek, $dayOfYear) - $year) { |
| 78 | case 1: |
| 79 | $date = $date->subWeeks(26); |
| 80 | break; |
| 81 | case -1: |
| 82 | $date = $date->addWeeks(26); |
| 83 | break; |
| 84 | } |
| 85 | $date = $date->addWeeks($week - $date->week(null, $dayOfWeek, $dayOfYear))->startOfWeek($dayOfWeek); |
| 86 | if ($date->dayOfWeek === $day) { |
| 87 | return $date; |
| 88 | } |
| 89 | return $date->next($day); |
| 90 | } |
| 91 | $year = $this->year; |
| 92 | $day = $this->dayOfYear; |
| 93 | $date = $this->avoidMutation()->dayOfYear($dayOfYear)->startOfWeek($dayOfWeek); |
| 94 | if ($date->year === $year && $day < $date->dayOfYear) { |
| 95 | return $year - 1; |
| 96 | } |
| 97 | $date = $this->avoidMutation()->addYear()->dayOfYear($dayOfYear)->startOfWeek($dayOfWeek); |
| 98 | if ($date->year === $year && $day >= $date->dayOfYear) { |
| 99 | return $year + 1; |
| 100 | } |
| 101 | return $year; |
| 102 | } |
| 103 | /** |
| 104 | * Get the number of weeks of the current week-year using given first day of week and first |
| 105 | * day of year included in the first week. Or use ISO format if no settings |
| 106 | * given. |
| 107 | * |
| 108 | * @param int|null $dayOfWeek first date of week from 0 (Sunday) to 6 (Saturday) |
| 109 | * @param int|null $dayOfYear first day of year included in the week #1 |
| 110 | * |
| 111 | * @return int |
| 112 | */ |
| 113 | public function isoWeeksInYear($dayOfWeek = null, $dayOfYear = null) |
| 114 | { |
| 115 | return $this->weeksInYear($dayOfWeek ?? 1, $dayOfYear ?? 4); |
| 116 | } |
| 117 | /** |
| 118 | * Get the number of weeks of the current week-year using given first day of week and first |
| 119 | * day of year included in the first week. Or use US format if no settings |
| 120 | * given (Sunday / Jan 6). |
| 121 | * |
| 122 | * @param int|null $dayOfWeek first date of week from 0 (Sunday) to 6 (Saturday) |
| 123 | * @param int|null $dayOfYear first day of year included in the week #1 |
| 124 | * |
| 125 | * @return int |
| 126 | */ |
| 127 | public function weeksInYear($dayOfWeek = null, $dayOfYear = null) |
| 128 | { |
| 129 | $dayOfWeek = $dayOfWeek ?? $this->getTranslationMessage('first_day_of_week') ?? 0; |
| 130 | $dayOfYear = $dayOfYear ?? $this->getTranslationMessage('day_of_first_week_of_year') ?? 1; |
| 131 | $year = $this->year; |
| 132 | $start = $this->avoidMutation()->dayOfYear($dayOfYear)->startOfWeek($dayOfWeek); |
| 133 | $startDay = $start->dayOfYear; |
| 134 | if ($start->year !== $year) { |
| 135 | $startDay -= $start->daysInYear; |
| 136 | } |
| 137 | $end = $this->avoidMutation()->addYear()->dayOfYear($dayOfYear)->startOfWeek($dayOfWeek); |
| 138 | $endDay = $end->dayOfYear; |
| 139 | if ($end->year !== $year) { |
| 140 | $endDay += $this->daysInYear; |
| 141 | } |
| 142 | return (int) \round(($endDay - $startDay) / 7); |
| 143 | } |
| 144 | /** |
| 145 | * Get/set the week number using given first day of week and first |
| 146 | * day of year included in the first week. Or use US format if no settings |
| 147 | * given (Sunday / Jan 6). |
| 148 | * |
| 149 | * @param int|null $week |
| 150 | * @param int|null $dayOfWeek |
| 151 | * @param int|null $dayOfYear |
| 152 | * |
| 153 | * @return int|static |
| 154 | */ |
| 155 | public function week($week = null, $dayOfWeek = null, $dayOfYear = null) |
| 156 | { |
| 157 | $date = $this; |
| 158 | $dayOfWeek = $dayOfWeek ?? $this->getTranslationMessage('first_day_of_week') ?? 0; |
| 159 | $dayOfYear = $dayOfYear ?? $this->getTranslationMessage('day_of_first_week_of_year') ?? 1; |
| 160 | if ($week !== null) { |
| 161 | return $date->addWeeks(\round($week) - $this->week(null, $dayOfWeek, $dayOfYear)); |
| 162 | } |
| 163 | $start = $date->avoidMutation()->dayOfYear($dayOfYear)->startOfWeek($dayOfWeek); |
| 164 | $end = $date->avoidMutation()->startOfWeek($dayOfWeek); |
| 165 | if ($start > $end) { |
| 166 | $start = $start->subWeeks(26)->dayOfYear($dayOfYear)->startOfWeek($dayOfWeek); |
| 167 | } |
| 168 | $week = (int) ($start->diffInDays($end) / 7 + 1); |
| 169 | return $week > $end->weeksInYear($dayOfWeek, $dayOfYear) ? 1 : $week; |
| 170 | } |
| 171 | /** |
| 172 | * Get/set the week number using given first day of week and first |
| 173 | * day of year included in the first week. Or use ISO format if no settings |
| 174 | * given. |
| 175 | * |
| 176 | * @param int|null $week |
| 177 | * @param int|null $dayOfWeek |
| 178 | * @param int|null $dayOfYear |
| 179 | * |
| 180 | * @return int|static |
| 181 | */ |
| 182 | public function isoWeek($week = null, $dayOfWeek = null, $dayOfYear = null) |
| 183 | { |
| 184 | return $this->week($week, $dayOfWeek ?? 1, $dayOfYear ?? 4); |
| 185 | } |
| 186 | } |
| 187 |