Core\Time\Duration::multipliedBy
$duration->multipliedBy(int $factor): Core\Time\Duration$duration->multipliedBy( int $factor, ): Core\Time\Duration
Returns a new duration that is the duration you call it on, multiplied by a whole number. The duration you call it on does not change.
Use it when one length repeats, for example the length of one lesson times the number of lessons, or a wait that doubles after each failed try. A factor of 0 gives a duration of zero. A negative factor gives a duration that points the other way.
The result must fit in a duration, which is about 292 years in each direction. A product that is longer throws a RuntimeError. The result is never shortened to make it fit.
Parameters
$factorintThe integer to multiply by;0gives an empty duration and a negative factor reverses the direction.
Return value
Returns Core\Time\Duration. A new Duration of $factor times the receiver; the receiver is unchanged.
Errors
RuntimeErrorThe product is longer than aDurationcan hold.
Examples
Repeat a length
<?nvs// Shows how to multiply a duration by a whole number.
Core\Time\Duration $lesson = 25m;
// Four lessons of 25 minutes take 100 minutes. This prints 1h40m.echo $lesson->multipliedBy(4)->toString(), "\n";
// A negative factor gives a negative duration. This prints -50m.echo $lesson->multipliedBy(-2)->toString(), "\n";
// A factor of 0 gives a duration of zero. This prints 0.echo $lesson->multipliedBy(0)->toSeconds(), "\n";1h40m-50m0A product that does not fit
<?nvs// Shows what happens when a product is too long for a duration.
// 106751 days is the largest number of whole days that fits. This prints 106751.echo 1d->multipliedBy(106751)->toSeconds() / 86400, "\n";
// One more day does not fit, so `multipliedBy` throws a `RuntimeError`.try { 1d->multipliedBy(106752);} catch (RuntimeError $e) { echo $e->message, "\n";}106751Core\Time\Duration::multipliedBy(): this duration is longer than `Core\Time\Duration` can holdWait longer after each failure
<?nvs// Computes how long a program waits before each new try, when every wait is twice the one before.
Core\Time\Duration $first = 500ms;
// The waits are 500ms, 1s, 2s, 4s and 8s. This prints each one.int $factor = 1;int $attempt = 1;while ($attempt <= 5) { echo $first->multipliedBy($factor)->toString(), "\n"; $factor = $factor * 2; $attempt = $attempt + 1;}500ms1s2s4s8s