Time\Duration::sub

(PHP 8 >= 8.6.0)

Time\Duration::sub — Subtracts a duration

Description

public function Time\Duration::sub(Time\Duration $duration): Time\Duration

Returns the difference between this duration and duration. The signs of both durations are taken into account, so subtracting a negative duration lengthens a positive one. If duration is longer than this duration, the result is negative.

Parameters

duration

The duration to subtract.

Return Values

Returns a new Time\Duration object.

Errors/Exceptions

Examples

Example #1 Time\Duration::sub() example

<?php
var_dump(\Time\Duration::fromSeconds(10)->sub(\Time\Duration::fromMilliseconds(2500)));

// Subtracting a larger duration results in a negative duration.
var_dump(\Time\Duration::fromSeconds(1)->sub(\Time\Duration::fromSeconds(3)));

The above example will output:

object(Time\Duration)#3 (3) {
  ["seconds"]=>
  int(7)
  ["nanoseconds"]=>
  int(500000000)
  ["negative"]=>
  bool(false)
}
object(Time\Duration)#1 (3) {
  ["seconds"]=>
  int(2)
  ["nanoseconds"]=>
  int(0)
  ["negative"]=>
  bool(true)
}

See Also

+add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top