Core\Time\DateTime::plus
$dateTime->plus(int $count, Core\Unit $unit): Core\Time\DateTime$dateTime->plus( int $count, Core\Unit $unit, ): Core\Time\DateTime
Returns a new date and time that is a number of units later, such as 3 days or 2 months.
You give a count and a unit from Core\Unit, such as Core\Unit::Hour. The value you call plus on does not change, and the result is in the same time zone. A negative count gives an earlier value.
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: one day is not always 24 hours. On a day when the clocks change, the day has 23 or 25 hours. Adding one day keeps the time of day. Adding 24 hours can give a different time of day.
Parameters
$countintHow many steps; negative moves back.$unitCore\UnitThe calendar unit of each step.
Return value
Returns Core\Time\DateTime. A new DateTime in the same zone; the receiver is unchanged.
Errors
RuntimeError$countis too large for a calendar step, or the result lies outside the representable range, about ±9999 years.
Examples
Add days hours and months
<?nvs// Shows how to move a date and time forward by days, hours and months.
var $start = Core\Time\DateTime::fromIso('2024-03-05 09:00', 'Europe/Berlin');
// `plus` returns a new value. The first argument is how many steps, and the second is the unit.// This prints 2024-03-15 09:00.echo $start->plus(10, Core\Unit::Day)->format('yyyy-MM-dd HH:mm'), "\n";
// This prints 2024-03-05 15:30.echo $start->plus(390, Core\Unit::Minute)->format('yyyy-MM-dd HH:mm'), "\n";
// This prints 2025-03-05 09:00.echo $start->plus(1, Core\Unit::Year)->format('yyyy-MM-dd HH:mm'), "\n";
// `$start` itself does not change. This prints 2024-03-05 09:00.echo $start->format('yyyy-MM-dd HH:mm'), "\n";2024-03-15 09:002024-03-05 15:302025-03-05 09:002024-03-05 09:00Month ends and clock changes
<?nvs// Shows what `plus` does at the end of a month and when the clocks change.
// One month after 31 January is the last day of February, because February has no 31st.// This prints 2024-02-29.var $january = Core\Time\DateTime::fromIso('2024-01-31', 'Europe/Berlin');echo $january->plus(1, Core\Unit::Month)->format('yyyy-MM-dd'), "\n";
// Berlin moves its clocks forward one hour on 31 March 2024, so that day has only 23 hours.// One day after 30 March at 12:00 is 31 March at 12:00. This prints 2024-03-31 12:00 +02:00.var $saturday = Core\Time\DateTime::fromIso('2024-03-30 12:00', 'Europe/Berlin');var $sunday = $saturday->plus(1, Core\Unit::Day);echo $sunday->format('yyyy-MM-dd HH:mm XXX'), "\n";
// 24 hours later is 13:00, because one hour of that night did not exist.// This prints 2024-03-31 13:00 +02:00.echo $saturday->plus(24, Core\Unit::Hour)->format('yyyy-MM-dd HH:mm XXX'), "\n";2024-02-292024-03-31 12:00 +02:002024-03-31 13:00 +02:00When a trial ends
<?nvs// Shows how to work out when a free trial ends and when the first payment is due.
// A customer starts a 14-day trial in Sydney, in the evening.var $signup = Core\Time\DateTime::fromIso('2024-01-20 21:15', 'Australia/Sydney');
// The trial ends 14 days later, at the same time of day. This prints Saturday 3 February, 21:15.var $trialEnd = $signup->plus(14, Core\Unit::Day);echo 'Trial ends: ', $trialEnd->format('EEEE d MMMM, HH:mm'), "\n";
// Each payment is due one month after the one before. The first is one month after the trial ends.// This prints 3 March 2024, 3 April 2024 and 3 May 2024.int $month = 1;while ($month <= 3) { echo 'Payment ', $month, ': ', $trialEnd->plus($month, Core\Unit::Month)->format('d MMMM yyyy'), "\n"; $month = $month + 1;}Trial ends: Saturday 3 February, 21:15Payment 1: 3 March 2024Payment 2: 3 April 2024Payment 3: 3 May 2024