Time\Duration::fromSeconds
(PHP 8 >= 8.6.0)
Time\Duration::fromSeconds — Creates a duration from a number of seconds and nanoseconds
Description
$seconds, int $nanoseconds = 0): Time\Duration
Creates a non-negative duration of seconds seconds
plus nanoseconds nanoseconds.
Parameters
seconds
The number of whole seconds.
Must be greater than or equal to 0.
nanoseconds
The fraction of a second, in nanoseconds.
Must be between 0 and 999999999,
inclusive.
Return Values
Returns a new Time\Duration object.
Errors/Exceptions
-
If
secondsis negative, a ValueError is thrown. -
If
nanosecondsis negative, a ValueError is thrown. -
If
nanosecondsis greater than or equal to1000000000, a ValueError is thrown. -
If the resulting duration exceeds the maximum length of a duration, a Time\TimeException is thrown.
Examples
Example #1 Time\Duration::fromSeconds() example
<?php
var_dump(\Time\Duration::fromSeconds(90));
var_dump(\Time\Duration::fromSeconds(1, 500_000_000));
try {
\Time\Duration::fromSeconds(0, 1_000_000_000);
} catch (ValueError $e) {
echo $e->getMessage(), PHP_EOL;
}The above example will output:
object(Time\Duration)#1 (3) {
["seconds"]=>
int(90)
["nanoseconds"]=>
int(0)
["negative"]=>
bool(false)
}
object(Time\Duration)#2 (3) {
["seconds"]=>
int(1)
["nanoseconds"]=>
int(500000000)
["negative"]=>
bool(false)
}
Time\Duration::fromSeconds(): Argument #2 ($nanoseconds) must be less than 1_000_000_000
See Also
- Time\Duration::fromNanoseconds() - Creates a duration from a number of nanoseconds
- Time\Duration::fromMicroseconds() - Creates a duration from a number of microseconds
- Time\Duration::fromMilliseconds() - Creates a duration from a number of milliseconds
- Time\Duration::fromMinutes() - Creates a duration from a number of minutes
- Time\Duration::fromHours() - Creates a duration from a number of hours
- Time\Duration::fromIso8601DurationString() - Creates a duration from an ISO 8601 duration string
来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。
