php8.5
Home/ Manual/ time / duration/ Time\Duration::divideBy

Time\Duration::divideBy

PHP function Edit on GitHub ✎

(PHP 8 >= 8.6.0)

Divides the duration by an integer divisor

Description

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

  • If divisor is negative, a ValueError is thrown.
  • If divisor is 0, a DivisionByZeroError is thrown.

Examples

Time\Duration::divideBy example

php
<?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:

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

See Also

Source: reference/datetime/time/duration/divideby.xml · from the official PHP manual (php/doc-en)