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

Time\Duration::multiplyBy

PHP function Edit on GitHub ✎

(PHP 8 >= 8.6.0)

Multiplies the duration by an integer factor

Description

Time\Duration::multiplyBy(int $factor): Time\Duration

Returns a duration that is factor times as long as this duration. The sign of the duration is preserved.

Parameters

factor

The factor to multiply by. Must be greater than or equal to 0; a factor of 0 results in a duration of zero.

Return Values

Returns a new Time\Duration object.

Errors/Exceptions

  • If factor is negative, a ValueError is thrown.

Examples

Time\Duration::multiplyBy example

php
<?php
var_dump(\Time\Duration::fromMilliseconds(1500)->multiplyBy(4));

try {
    \Time\Duration::fromHours(1)->multiplyBy(3_000_000);
} catch (\Time\TimeException $e) {
    echo $e->getMessage(), PHP_EOL;
}

The above example will output:

output
object(Time\Duration)#2 (3) {
  ["seconds"]=>
  int(6)
  ["nanoseconds"]=>
  int(0)
  ["negative"]=>
  bool(false)
}
The maximum representable range is 9_223_372_035 seconds (roughly 292 years)

See Also

  • Time\Duration::divideBy
  • Time\Duration::add

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