Core\Time\Instant::plus
$instant->plus(Core\Time\Duration $d): Core\Time\Instant$instant->plus( Core\Time\Duration $d, ): Core\Time\Instant
Moves this instant forward by an exact Duration and returns the new instant.
The original instant does not change. plus(1h) returns the moment exactly 3600 seconds later, and plus(24h) returns the moment exactly 24 hours later. An instant has no time zone, so a change to or from summer time does not change the result. On a local clock, the same step can show 23 or 25 hours on the day the clock changes. A negative duration moves the instant back.
The result must be between the years -9999 and 9999. Outside that range, plus throws a RuntimeError.
To add calendar days or months, use a DateTime in a time zone instead.
Parameters
$dCore\Time\DurationThe duration to add. A negative duration moves the instant back.
Return value
Returns Core\Time\Instant. A new Instant. The original instant does not change.
Errors
RuntimeErrorThe result is before the year -9999 or after the year 9999.
Examples
Thirty minutes later
<?nvs// Shows how to get the moment that is a fixed amount of time after another one.
Core\Time\Instant $start = Core\Time\Instant::fromIso('2024-03-01T14:00:00Z');
// This prints 2024-03-01T14:30:00Z.echo $start->plus(30m)->toIso(), "\n";
// `plus` returns a new instant. `$start` does not change, so this prints 2024-03-01T14:00:00Z.echo $start->toIso(), "\n";2024-03-01T14:30:00Z2024-03-01T14:00:00ZAcross a summer time change
<?nvs// Shows that `plus` adds real hours, even on the night the local clock changes.
Core\Time\Zone $berlin = Core\Time\Zone::of('Europe/Berlin');
// On 31 March 2024, clocks in Berlin moved from 02:00 to 03:00.Core\Time\Instant $evening = Core\Time\Instant::fromIso('2024-03-30T20:00:00Z');Core\Time\Instant $next = $evening->plus(24h);
// Exactly 24 hours pass, but the local clock shows one hour more.// This prints 21:00 +01:00 and then 22:00 +02:00.echo $evening->in($berlin)->format('HH:mm XXX'), "\n";echo $next->in($berlin)->format('HH:mm XXX'), "\n";21:00 +01:0022:00 +02:00When a login link expires
<?nvs// Sets the time a login link stops working, and checks it when the link is used.
// A real program uses `Core\Time::now()` here. A fixed time keeps the output the same.Core\Time\Instant $sent = Core\Time\Instant::fromIso('2024-03-01T09:00:00Z');Core\Time\Instant $expires = $sent->plus(15m);
// This prints 2024-03-01T09:15:00Z.echo 'valid until ', $expires->toIso(), "\n";
array<string> $clicks = ['2024-03-01T09:14:59Z', '2024-03-01T09:15:00Z', '2024-03-01T10:02:00Z'];foreach ($clicks as string $at) { // The link works only before `$expires`. This prints ok, expired and expired. if (Core\Time\Instant::fromIso($at) < $expires) { echo $at, " ok\n"; } else { echo $at, " expired\n"; }}valid until 2024-03-01T09:15:00Z2024-03-01T09:14:59Z ok2024-03-01T09:15:00Z expired2024-03-01T10:02:00Z expired