Skip to content

Core\Time\Date::next

Returns the first date after this one that is on a given day of the week. You give the day as a case of Core\Time\Weekday, such as Weekday::Friday.

The result is one to seven days later. When the date is already on that weekday, next returns the same weekday one week later. To include the date itself, call next on the day before: $date->minus(1, Unit::Day)->next(Weekday::Friday).

The result is a new Date, and the date you called it on does not change. next throws a RuntimeError only when the result is after the year 9999.

Good to know: previous finds the weekday before a date.

Parameters

  • $wCore\Time\WeekdayThe weekday to move to.

Return value

Returns Core\Time\Date. A new Date one to seven days later. It is seven days later when this date is already on $w.

Errors

  • RuntimeErrorThe result is after the year 9999.

Changelog

VersionChange
0.0.6Core\Time\Date::startOf and endOf return the first or last day of the date's week, month, quarter or year. difference counts whole days, weeks, months, quarters or years between two dates. next and previous find the nearest date on a given weekday.

Examples

The next friday

<?nvs
// Shows how to find the next Friday after a date.
use Core\Time\Weekday;
// 20 May 2026 is a Wednesday.
var $today = Core\Time\Date::at(2026, 5, 20);
// The next Friday is two days later. This prints Fri 2026-05-22.
echo $today->next(Weekday::Friday)->format('EEE yyyy-MM-dd'), "\n";
// The next Monday is in the next week. This prints Mon 2026-05-25.
echo $today->next(Weekday::Monday)->format('EEE yyyy-MM-dd'), "\n";
Output
Fri 2026-05-22
Mon 2026-05-25

The same weekday

<?nvs
// Shows what `next` returns when the date is already on the weekday you give.
use Core\Time\Weekday;
// 20 May 2026 is a Wednesday.
var $wednesday = Core\Time\Date::at(2026, 5, 20);
// `next` always moves forward, so the result is one week later. This prints 2026-05-27.
echo $wednesday->next(Weekday::Wednesday)->toIso(), "\n";
// To keep the date itself, start from the day before. This prints 2026-05-20.
echo $wednesday->minus(1, Core\Unit::Day)->next(Weekday::Wednesday)->toIso(), "\n";
Output
2026-05-27
2026-05-20

The next team meetings

<?nvs
// Shows how to list the dates of a meeting that takes place every Tuesday.
var $day = Core\Time\Date::at(2026, 12, 24);
int $i = 0;
while ($i < 4) {
// Each call moves to the next Tuesday after the date before it.
$day = $day->next(Core\Time\Weekday::Tuesday);
echo 'Meeting on ', $day->format('EEEE d MMMM yyyy'), "\n";
$i = $i + 1;
}
Output
Meeting on Tuesday 29 December 2026
Meeting on Tuesday 5 January 2027
Meeting on Tuesday 12 January 2027
Meeting on Tuesday 19 January 2027