Date Units
use Derafu\Date\Enum\DateUnit;
use Derafu\Date\Enum\Weekday;
DateUnit
DateUnit is an enum of the units of time. The value of each case is its one-letter code.
| Case | Code | Months |
|---|---|---|
DAY |
D |
|
WEEK |
W |
|
MONTH |
M |
1 |
QUARTER |
Q |
3 |
SEMESTER |
S |
6 |
YEAR |
Y |
12 |
DateUnit::fromCode('m') gives the unit of a code in upper or lower case, and an InvalidDateException if there is none. getMonths() gives the months of the unit, or null for days and weeks.
Move a date
DateUnit::MONTH->add($date, 1); // a month later
DateUnit::QUARTER->subtract($date, 2); // two quarters earlier
DateUnit::DAY->add($date, -3); // a negative number goes the other way
add($date, $steps = 1) and subtract($date, $steps = 1) give a new DateTimeImmutable; the date that is given does not change and its time is kept.
Months, quarters, semesters and years never overflow: the day that the month of the result does not have is the last day of that month.
| Date | Move | Result |
|---|---|---|
2024-01-31 |
+ 1 month | 2024-02-29 |
2023-01-31 |
+ 1 month | 2023-02-28 |
2024-08-31 |
+ 1 semester | 2025-02-28 |
2024-02-29 |
+ 1 year | 2025-02-28 |
Weekday
Weekday numbers the days of the week like ISO 8601: Monday is 1 and Sunday is 7. Weekday::fromDate($date) gives the day of the week of a date. A work calendar uses it to say which days are the weekend.