Skip to content

Core\Time\DateTime::next

Returns a new date and time on the next day that falls on a given weekday.

You give a case of Core\Time\Weekday, such as Core\Time\Weekday::Monday. The result is the nearest later day with that weekday. The time of day and the time zone stay the same, and the value you call next on does not change.

next always moves forward, by one to seven days. When the value is already on that weekday, the result is seven days later.

Use it for work that repeats every week, such as a report that runs each Monday at 06:00. The result keeps that time of day in a week when the clocks change.

Parameters

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

Return value

Returns Core\Time\DateTime. A new DateTime one to seven days later — a full week when the receiver already falls on $w; the receiver is unchanged.

Errors

  • RuntimeErrorThe result lies outside the representable range.

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

Find the next monday

<?nvs
// Shows how to find the next day that falls on a given weekday.
// 12 June 2024 is a Wednesday.
var $wednesday = Core\Time\DateTime::fromIso('2024-06-12 14:45', 'Europe/Paris');
// `next` returns a new value on the next Monday. The time of day stays the same.
// This prints Monday 17 June 2024, 14:45.
echo $wednesday->next(Core\Time\Weekday::Monday)->format('EEEE d MMMM yyyy, HH:mm'), "\n";
// The next Friday is in the same week. This prints Friday 14 June 2024, 14:45.
echo $wednesday->next(Core\Time\Weekday::Friday)->format('EEEE d MMMM yyyy, HH:mm'), "\n";
Output
Monday 17 June 2024, 14:45
Friday 14 June 2024, 14:45

The same weekday is a week later

<?nvs
// Shows what `next` returns when the value already falls on that weekday.
// 30 December 2024 is a Monday.
var $monday = Core\Time\DateTime::fromIso('2024-12-30 08:00', 'Asia/Tokyo');
// `next` always moves forward. On a Monday, the next Monday is seven days later.
// This prints 2025-01-06 08:00, which is in the next year.
echo $monday->next(Core\Time\Weekday::Monday)->format('yyyy-MM-dd HH:mm'), "\n";
// The value itself does not change. This prints 2024-12-30 08:00.
echo $monday->format('yyyy-MM-dd HH:mm'), "\n";
Output
2025-01-06 08:00
2024-12-30 08:00

When a weekly report runs

<?nvs
// Shows how to list the next times a weekly report runs.
// The report runs every Monday at 06:00 in Chicago. The last run was on Monday 21 October 2024.
var $run = Core\Time\DateTime::fromIso('2024-10-21 06:00', 'America/Chicago');
// Each run is the next Monday after the one before. The clocks change on 3 November, but each run is at 06:00.
// This prints 28 October, 4 November and 11 November, each at 06:00.
int $count = 0;
while ($count < 3) {
$run = $run->next(Core\Time\Weekday::Monday);
echo $run->format('EEEE d MMMM, HH:mm XXX'), "\n";
$count = $count + 1;
}
Output
Monday 28 October, 06:00 -05:00
Monday 4 November, 06:00 -06:00
Monday 11 November, 06:00 -06:00