Core\Time\Instant::minus
$instant->minus(Core\Time\Duration $d): Core\Time\Instant$instant->minus( Core\Time\Duration $d, ): Core\Time\Instant
Moves this instant back by an exact Duration and returns the new instant.
The original instant does not change. minus(1h) returns the moment exactly 3600 seconds earlier, and minus(24h) returns the moment exactly 24 hours earlier. An instant has no time zone, so a change to or from summer time does not change the result. A negative duration moves the instant forward.
The result must be between the years -9999 and 9999. Outside that range, minus throws a RuntimeError.
To count calendar days or months, use a DateTime in a time zone instead.
Parameters
$dCore\Time\DurationThe exact duration to subtract; a negative one moves the instant forward.
Return value
Returns Core\Time\Instant. A new Instant; the receiver is unchanged.
Errors
RuntimeErrorThe result lies outside the representable range, about ±9999 years.
Examples
An hour and a half earlier
<?nvs// Shows how to get the moment that is a fixed amount of time before another one.
Core\Time\Instant $meeting = Core\Time\Instant::fromIso('2024-03-01T14:00:00Z');
// This prints 2024-03-01T12:30:00Z.echo $meeting->minus(1h30m)->toIso(), "\n";
// `minus` returns a new instant. `$meeting` does not change, so this prints 2024-03-01T14:00:00Z.echo $meeting->toIso(), "\n";2024-03-01T12:30:00Z2024-03-01T14:00:00ZA negative duration
<?nvs// Shows that subtracting a negative duration moves an instant forward.
Core\Time\Instant $start = Core\Time\Instant::fromEpoch(0);
// `negated` turns 15 minutes into minus 15 minutes. This prints 1970-01-01T00:15:00Z.echo $start->minus(15m->negated())->toIso(), "\n";
// Subtracting 1 nanosecond goes back past midnight. This prints 1969-12-31T23:59:59.999999999Z.echo $start->minus(1ns)->toIso(), "\n";1970-01-01T00:15:00Z1969-12-31T23:59:59.999999999ZOrders from the last 24 hours
<?nvs// Finds the orders placed in the last 24 hours, as a daily report does.
// A real program uses `Core\Time::now()` here. A fixed time keeps the output the same.Core\Time\Instant $now = Core\Time\Instant::fromIso('2024-03-02T09:00:00Z');Core\Time\Instant $since = $now->minus(24h);
array<string> $placed = [ 'A-1001' => '2024-02-29T17:20:00Z', 'A-1002' => '2024-03-01T08:59:59Z', 'A-1003' => '2024-03-01T09:00:00Z', 'A-1004' => '2024-03-02T07:45:00Z',];
foreach ($placed as string $order => string $at) { // `compareTo` returns 0 or 1 when the order is at or after `$since`. if (Core\Time\Instant::fromIso($at)->compareTo($since) >= 0) { echo $order, "\n"; }}// This prints A-1003 and A-1004. A-1002 is one second too early.A-1003A-1004