Skip to content

Core\Time\DateTime::weekday

$dateTime->weekday(): Core\Time\Weekday

Returns the day of the week of a date and time, as a case of the Core\Time\Weekday enum.

The result is one of seven cases, from Core\Time\Weekday::Monday to Core\Time\Weekday::Sunday. You compare it with ==, for example to check whether a delivery falls on a weekend.

weekday reads the day in the time zone of the value. One moment can be a Sunday in Los Angeles and already a Monday in Tokyo.

Good to know: the result is not a number. If you need one, as int converts the case to its position in the week, and Monday is 0. To print the name of the day, use format with EEEE.

Return value

Returns Core\Time\Weekday. The Weekday case, Weekday::Monday through Weekday::Sunday.

Changelog

VersionChange
0.0.5Core\Time\DateTime::weekday returns a Core\Time\Weekday case. Core\Time\DateTime::next and Core\Time\DateTime::previous take a Core\Time\Weekday case.

Examples

The day of the week

<?nvs
// Shows how to find the day of the week of a date and time.
var $meeting = Core\Time\DateTime::fromIso('2024-03-05 14:30', 'Europe/Berlin');
// `weekday` returns a case of the `Core\Time\Weekday` enum. You can compare it with `==`.
// This prints Tuesday.
if ($meeting->weekday() == Core\Time\Weekday::Tuesday) {
echo "Tuesday\n";
}
// `as int` gives the position in the week, and Monday is 0. This prints 1.
echo $meeting->weekday() as int, "\n";
Output
Tuesday
1

The weekday depends on the time zone

<?nvs
// Shows that one moment can be a different day of the week in another time zone.
// A video call starts at 18:00 on Sunday in Los Angeles.
var $call = Core\Time\DateTime::fromIso('2024-09-01 18:00', 'America/Los_Angeles');
// `weekday` reads the day in the time zone of the value. This prints yes.
echo $call->weekday() == Core\Time\Weekday::Sunday ? 'yes' : 'no', "\n";
// At that moment it is 10:00 on Monday in Tokyo. This prints yes.
var $tokyo = $call->toInstant()->in('Asia/Tokyo');
echo $tokyo->weekday() == Core\Time\Weekday::Monday ? 'yes' : 'no', "\n";
Output
yes
yes

Move a delivery off the weekend

<?nvs
// Shows how to move a delivery date to Monday when it falls on a weekend.
var $warehouse = Core\Time\Zone::of('Europe/Madrid');
array<string> $planned = ['2024-05-17T08:00:00Z', '2024-05-18T08:00:00Z', '2024-05-19T08:00:00Z'];
// A Saturday or a Sunday moves to the next Monday. `next` finds the next day with that weekday.
// This prints Friday 17 May, then Monday 20 May twice.
foreach ($planned as string $text) {
var $delivery = Core\Time\Instant::fromIso($text)->in($warehouse);
var $day = $delivery->weekday();
if ($day == Core\Time\Weekday::Saturday || $day == Core\Time\Weekday::Sunday) {
$delivery = $delivery->next(Core\Time\Weekday::Monday);
}
echo $delivery->format('EEEE d MMMM'), "\n";
}
Output
Friday 17 May
Monday 20 May
Monday 20 May