Time\Duration::divideBy

(PHP 8 >= 8.6.0)

Time\Duration::divideBy — Divides the duration by an integer divisor

Description

public function Time\Duration::divideBy(int $divisor): Time\Duration

Returns a duration that is this duration divided by divisor. Any remaining fraction of a nanosecond is truncated, so the result is rounded towards zero. The sign of the duration is preserved.

Parameters

divisor

The divisor. Must be greater than 0.

Return Values

Returns a new Time\Duration object.

Errors/Exceptions

Examples

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

<?php
// Fractions of a nanosecond are truncated.
var_dump(\Time\Duration::fromSeconds(10)->divideBy(3)->multiplyBy(3));

try {
    \Time\Duration::fromSeconds(10)->divideBy(0);
} catch (DivisionByZeroError $e) {
    echo $e->getMessage(), PHP_EOL;
}

The above example will output:

object(Time\Duration)#3 (3) {
  ["seconds"]=>
  int(9)
  ["nanoseconds"]=>
  int(999999999)
  ["negative"]=>
  bool(false)
}
Division by zero

See Also

+add a note

User Contributed Notes

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