Time\Duration::sub
(PHP 8 >= 8.6.0)
Time\Duration::sub — Subtracts a duration
Description
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
-
If the resulting duration exceeds the maximum length of a duration, a Time\TimeException is thrown.
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
- Time\Duration::add() - Adds a duration
- Time\Duration::negate() - Returns the duration with the opposite sign
- Time\Duration::absolute() - Returns the absolute value of the duration
来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。
