Skip to content

Core\Time\Weekday

A day of the week: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday or Sunday.

Core\Time\Weekday is declared in the namespace Core\Time, because the classes DateTime and Date both use it. The methods weekday, next and previous use it.

Monday comes first, the way calendars in most of the world print it and the way the ISO week counts it. Saturday and Sunday are the last two. The method weekday returns one of these cases. The methods that move to a named day, such as the next Friday or the previous Monday, take one.

Good to know: a move to a named day never stays on the same day. The next Friday of a Friday is one week later, which is what a person means by "next Friday". If you want the day itself when it already matches, check the weekday first and move only when it does not match.

Examples

A week starts on monday

<?nvs
// A week, Monday first. Each date is asked which day of the week it falls on,
// and the answer is one of the seven cases.
class Week
{
public static function name(Core\Time\Weekday $day): string
{
return match ($day) {
Core\Time\Weekday::Monday => 'Monday',
Core\Time\Weekday::Tuesday => 'Tuesday',
Core\Time\Weekday::Wednesday => 'Wednesday',
Core\Time\Weekday::Thursday => 'Thursday',
Core\Time\Weekday::Friday => 'Friday',
Core\Time\Weekday::Saturday => 'Saturday',
Core\Time\Weekday::Sunday => 'Sunday',
};
}
// The two days at the end of the week are the ones most offices are shut.
public static function isWeekend(Core\Time\Weekday $day): bool
{
return $day == Core\Time\Weekday::Saturday || $day == Core\Time\Weekday::Sunday;
}
}
var $utc = Core\Time\Zone::UTC;
var $monday = Core\Time\DateTime::fromIso('2024-05-13', $utc);
int $step = 0;
while ($step < 7) {
var $date = $monday->plus($step, Core\Unit::Day);
var $day = $date->weekday();
echo $date->format('yyyy-MM-dd'), ' is a ', Week::name($day),
Week::isWeekend($day) ? ' (weekend)' : '',
"\n";
$step = $step + 1;
}
Output
2024-05-13 is a Monday
2024-05-14 is a Tuesday
2024-05-15 is a Wednesday
2024-05-16 is a Thursday
2024-05-17 is a Friday
2024-05-18 is a Saturday (weekend)
2024-05-19 is a Sunday (weekend)

The next friday is never today

<?nvs
// `next` and `previous` move a date to a given day of the week.
// They always move at least one day. So `next(Core\Time\Weekday::Friday)` on a Friday
// gives the Friday a week later.
var $utc = Core\Time\Zone::UTC;
var $day = 'yyyy-MM-dd';
var $friday = Core\Time\DateTime::fromIso('2024-05-17', $utc);
echo 'today is ', $friday->format($day), ", a Friday\n";
echo 'next Friday ', $friday->next(Core\Time\Weekday::Friday)->format($day), "\n";
echo 'next Monday ', $friday->next(Core\Time\Weekday::Monday)->format($day), "\n";
echo 'the Monday just gone ', $friday->previous(Core\Time\Weekday::Monday)->format($day), "\n\n";
// To keep today when it is already a Friday, check `weekday()` first.
// This code calls `next` only when today is a different day.
var $wanted = Core\Time\Weekday::Friday;
var $thisOrNext = $friday->weekday() == $wanted ? $friday : $friday->next($wanted);
echo 'this Friday, or the next one: ', $thisOrNext->format($day), "\n";
Output
today is 2024-05-17, a Friday
next Friday 2024-05-24
next Monday 2024-05-20
the Monday just gone 2024-05-13
this Friday, or the next one: 2024-05-17

Payday is the last friday of the month

<?nvs
// Payday is the last Friday of the month. Take the last day of the month and,
// unless it is already a Friday, step back to the Friday before it.
class Payroll
{
public static function payday(Core\Time\DateTime $anyDayThatMonth): Core\Time\DateTime
{
var $last = $anyDayThatMonth->endOf(Core\Unit::Month)->startOf(Core\Unit::Day);
if ($last->weekday() == Core\Time\Weekday::Friday) {
return $last;
}
return $last->previous(Core\Time\Weekday::Friday);
}
}
var $utc = Core\Time\Zone::UTC;
uint $month = 1;
while ($month <= 6) {
var $someDay = Core\Time\DateTime::at(Core\Time\Date::at(2024, $month, 1), Core\Time\TimeOfDay::at(0, 0), $utc);
var $payday = Payroll::payday($someDay);
echo $someDay->format('MMMM'), ': paid on ', $payday->format('yyyy-MM-dd'),
', ', $payday->difference($someDay->endOf(Core\Unit::Month), Core\Unit::Day),
" day(s) before the month is out\n";
$month = $month + 1;
}
Output
January: paid on 2024-01-26, 5 day(s) before the month is out
February: paid on 2024-02-23, 6 day(s) before the month is out
March: paid on 2024-03-29, 2 day(s) before the month is out
April: paid on 2024-04-26, 4 day(s) before the month is out
May: paid on 2024-05-31, 0 day(s) before the month is out
June: paid on 2024-06-28, 2 day(s) before the month is out

Changelog

VersionChange
0.0.5Core\Time\Weekday is the enum of the days of the week, from Monday to Sunday. Core\Time\Date::weekday and Core\Time\DateTime::weekday return one of its cases.