blob: 64cece7a02852541a09a23cbee20325932532c5b [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?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 */
11namespace 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 */
36trait Week
37{
38 /**
39 * Set/get the week number of year using given first day of week and first
40 * day of year included in the first week. Or use ISO format if no settings
41 * given.
42 *
43 * @param int|null $year if null, act as a getter, if not null, set the year and return current instance.
44 * @param int|null $dayOfWeek first date of week from 0 (Sunday) to 6 (Saturday)
45 * @param int|null $dayOfYear first day of year included in the week #1
46 *
47 * @return int|static
48 */
49 public function isoWeekYear($year = null, $dayOfWeek = null, $dayOfYear = null)
50 {
51 return $this->weekYear(
52 $year,
53 $dayOfWeek ?? 1,
54 $dayOfYear ?? 4
55 );
56 }
57
58 /**
59 * Set/get the week number of year using given first day of week and first
60 * day of year included in the first week. Or use US format if no settings
61 * given (Sunday / Jan 6).
62 *
63 * @param int|null $year if null, act as a getter, if not null, set the year and return current instance.
64 * @param int|null $dayOfWeek first date of week from 0 (Sunday) to 6 (Saturday)
65 * @param int|null $dayOfYear first day of year included in the week #1
66 *
67 * @return int|static
68 */
69 public function weekYear($year = null, $dayOfWeek = null, $dayOfYear = null)
70 {
71 $dayOfWeek = $dayOfWeek ?? $this->getTranslationMessage('first_day_of_week') ?? 0;
72 $dayOfYear = $dayOfYear ?? $this->getTranslationMessage('day_of_first_week_of_year') ?? 1;
73
74 if ($year !== null) {
75 $year = (int) round($year);
76
77 if ($this->weekYear(null, $dayOfWeek, $dayOfYear) === $year) {
78 return $this->avoidMutation();
79 }
80
81 $week = $this->week(null, $dayOfWeek, $dayOfYear);
82 $day = $this->dayOfWeek;
83 $date = $this->year($year);
84 switch ($date->weekYear(null, $dayOfWeek, $dayOfYear) - $year) {
85 case 1:
86 $date = $date->subWeeks(26);
87
88 break;
89 case -1:
90 $date = $date->addWeeks(26);
91
92 break;
93 }
94
95 $date = $date->addWeeks($week - $date->week(null, $dayOfWeek, $dayOfYear))->startOfWeek($dayOfWeek);
96
97 if ($date->dayOfWeek === $day) {
98 return $date;
99 }
100
101 return $date->next($day);
102 }
103
104 $year = $this->year;
105 $day = $this->dayOfYear;
106 $date = $this->avoidMutation()->dayOfYear($dayOfYear)->startOfWeek($dayOfWeek);
107
108 if ($date->year === $year && $day < $date->dayOfYear) {
109 return $year - 1;
110 }
111
112 $date = $this->avoidMutation()->addYear()->dayOfYear($dayOfYear)->startOfWeek($dayOfWeek);
113
114 if ($date->year === $year && $day >= $date->dayOfYear) {
115 return $year + 1;
116 }
117
118 return $year;
119 }
120
121 /**
122 * Get the number of weeks of the current week-year using given first day of week and first
123 * day of year included in the first week. Or use ISO format if no settings
124 * given.
125 *
126 * @param int|null $dayOfWeek first date of week from 0 (Sunday) to 6 (Saturday)
127 * @param int|null $dayOfYear first day of year included in the week #1
128 *
129 * @return int
130 */
131 public function isoWeeksInYear($dayOfWeek = null, $dayOfYear = null)
132 {
133 return $this->weeksInYear(
134 $dayOfWeek ?? 1,
135 $dayOfYear ?? 4
136 );
137 }
138
139 /**
140 * Get the number of weeks of the current week-year using given first day of week and first
141 * day of year included in the first week. Or use US format if no settings
142 * given (Sunday / Jan 6).
143 *
144 * @param int|null $dayOfWeek first date of week from 0 (Sunday) to 6 (Saturday)
145 * @param int|null $dayOfYear first day of year included in the week #1
146 *
147 * @return int
148 */
149 public function weeksInYear($dayOfWeek = null, $dayOfYear = null)
150 {
151 $dayOfWeek = $dayOfWeek ?? $this->getTranslationMessage('first_day_of_week') ?? 0;
152 $dayOfYear = $dayOfYear ?? $this->getTranslationMessage('day_of_first_week_of_year') ?? 1;
153 $year = $this->year;
154 $start = $this->avoidMutation()->dayOfYear($dayOfYear)->startOfWeek($dayOfWeek);
155 $startDay = $start->dayOfYear;
156 if ($start->year !== $year) {
157 $startDay -= $start->daysInYear;
158 }
159 $end = $this->avoidMutation()->addYear()->dayOfYear($dayOfYear)->startOfWeek($dayOfWeek);
160 $endDay = $end->dayOfYear;
161 if ($end->year !== $year) {
162 $endDay += $this->daysInYear;
163 }
164
165 return (int) round(($endDay - $startDay) / 7);
166 }
167
168 /**
169 * Get/set the week number using given first day of week and first
170 * day of year included in the first week. Or use US format if no settings
171 * given (Sunday / Jan 6).
172 *
173 * @param int|null $week
174 * @param int|null $dayOfWeek
175 * @param int|null $dayOfYear
176 *
177 * @return int|static
178 */
179 public function week($week = null, $dayOfWeek = null, $dayOfYear = null)
180 {
181 $date = $this;
182 $dayOfWeek = $dayOfWeek ?? $this->getTranslationMessage('first_day_of_week') ?? 0;
183 $dayOfYear = $dayOfYear ?? $this->getTranslationMessage('day_of_first_week_of_year') ?? 1;
184
185 if ($week !== null) {
186 return $date->addWeeks(round($week) - $this->week(null, $dayOfWeek, $dayOfYear));
187 }
188
189 $start = $date->avoidMutation()->dayOfYear($dayOfYear)->startOfWeek($dayOfWeek);
190 $end = $date->avoidMutation()->startOfWeek($dayOfWeek);
191 if ($start > $end) {
192 $start = $start->subWeeks(26)->dayOfYear($dayOfYear)->startOfWeek($dayOfWeek);
193 }
194 $week = (int) ($start->diffInDays($end) / 7 + 1);
195
196 return $week > $end->weeksInYear($dayOfWeek, $dayOfYear) ? 1 : $week;
197 }
198
199 /**
200 * Get/set the week number using given first day of week and first
201 * day of year included in the first week. Or use ISO format if no settings
202 * given.
203 *
204 * @param int|null $week
205 * @param int|null $dayOfWeek
206 * @param int|null $dayOfYear
207 *
208 * @return int|static
209 */
210 public function isoWeek($week = null, $dayOfWeek = null, $dayOfYear = null)
211 {
212 return $this->week(
213 $week,
214 $dayOfWeek ?? 1,
215 $dayOfYear ?? 4
216 );
217 }
218}