Core\Time\Date::plus
$date->plus(int $count, Core\Unit $unit): Core\Time\Date$date->plus( int $count, Core\Unit $unit, ): Core\Time\Date
Returns a new date that is a number of days, weeks, months, quarters or years later.
You give a count and a unit from Core\Unit, such as Core\Unit::Week. The date you call plus on does not change. A negative count gives an earlier date.
When you add months, quarters or years, the day of the month stays the same if the new month has that day. If it does not, the result is the last day of that month. 31 January 2024 plus one month is 29 February 2024.
Good to know: a date has no time of day. A unit smaller than a day, such as Core\Unit::Minute, throws a LogicError.
Parameters
$countintHow many steps; negative moves back.$unitCore\UnitUnit::Day,Unit::Week,Unit::Month,Unit::QuarterorUnit::Year.
Return value
Returns Core\Time\Date. A new Date; the receiver is unchanged.
Errors
LogicError$unitis smaller thanUnit::Day, which does not move a date.RuntimeError$countis too large for a calendar step, or the result lies outside-9999..=9999.
Examples
Days and weeks ahead
<?nvs// Shows how to move a date forward by days and by weeks.
var $ordered = Core\Time\Date::at(2024, 3, 10);
// `plus` returns a new date that is 5 days later. This prints 2024-03-15.echo $ordered->plus(5, Core\Unit::Day)->format('yyyy-MM-dd'), "\n";
// A step can go past the end of a month or a year.// Two weeks after 25 December 2024 is 2025-01-08.echo Core\Time\Date::at(2024, 12, 25)->plus(2, Core\Unit::Week)->format('yyyy-MM-dd'), "\n";
// A negative count moves the date back. This prints 2024-03-07.echo $ordered->plus(-3, Core\Unit::Day)->format('yyyy-MM-dd'), "\n";
// `plus` does not change the date it is called on. This prints 2024-03-10.echo $ordered->format('yyyy-MM-dd'), "\n";2024-03-152025-01-082024-03-072024-03-10A day the month does not have
<?nvs// Shows what `plus` does when the day does not exist in the month it reaches.
// January has 31 days, but February 2024 has only 29.// The result is the last day of February. This prints 2024-02-29.echo Core\Time\Date::at(2024, 1, 31)->plus(1, Core\Unit::Month)->format('yyyy-MM-dd'), "\n";
// A quarter is three months. 30 November plus one quarter is 2025-02-28.echo Core\Time\Date::at(2024, 11, 30)->plus(1, Core\Unit::Quarter)->format('yyyy-MM-dd'), "\n";
// 29 February exists only in a leap year. One year later, the result is 28 February.// This prints 2025-02-28.echo Core\Time\Date::at(2024, 2, 29)->plus(1, Core\Unit::Year)->format('yyyy-MM-dd'), "\n";
// A date has no time of day, so `Core\Unit::Minute` throws a `LogicError`.try { Core\Time\Date::at(2024, 1, 31)->plus(90, Core\Unit::Minute);} catch (LogicError $error) { echo 'Not allowed: ', $error->message, "\n";}2024-02-292025-02-282025-02-28Not allowed: Core\Time\Date::plus(): a unit smaller than `Unit::Day` does not move a dateMonthly renewal dates
<?nvs// Shows how to list the renewal dates of a monthly subscription.
// The subscription starts on 31 January. Each renewal is counted from the start date.// A month without a 31st gets its last day, and the next month is back on the 31st.var $start = Core\Time\Date::at(2024, 1, 31);int $month = 1;while ($month <= 4) { echo 'Renewal ', $month, ': ', $start->plus($month, Core\Unit::Month)->format('yyyy-MM-dd'), "\n"; $month = $month + 1;}
// Adding one month to the previous renewal gives a different result.// The day stays at the 29th. This prints 2024-03-29.echo $start->plus(1, Core\Unit::Month)->plus(1, Core\Unit::Month)->format('yyyy-MM-dd'), "\n";Renewal 1: 2024-02-29Renewal 2: 2024-03-31Renewal 3: 2024-04-30Renewal 4: 2024-05-312024-03-29