Time\Duration::fromNanoseconds
(PHP 8 >= 8.6.0)
Time\Duration::fromNanoseconds — Creates a duration from a number of nanoseconds
Description
Creates a non-negative duration of nanoseconds nanoseconds.
The value is normalized into whole seconds and nanoseconds, see the
Time\Duration::$seconds and
Time\Duration::$nanoseconds properties.
Parameters
nanoseconds
The number of nanoseconds.
Must be greater than or equal to 0.
Return Values
Returns a new Time\Duration object.
Errors/Exceptions
-
If
nanosecondsis negative, a ValueError is thrown.
Examples
Example #1 Time\Duration::fromNanoseconds() example
<?php
var_dump(\Time\Duration::fromNanoseconds(1_500_000_000));The above example will output:
object(Time\Duration)#1 (3) {
["seconds"]=>
int(1)
["nanoseconds"]=>
int(500000000)
["negative"]=>
bool(false)
}
Example #2 Measuring elapsed time with hrtime()
<?php
$start = hrtime(true);
usleep(1000);
$elapsed = \Time\Duration::fromNanoseconds(hrtime(true) - $start);
var_dump($elapsed);The above example will output something similar to:
object(Time\Duration)#1 (3) {
["seconds"]=>
int(0)
["nanoseconds"]=>
int(1062541)
["negative"]=>
bool(false)
}
See Also
- Time\Duration::fromSeconds() - Creates a duration from a number of seconds and nanoseconds
- Time\Duration::fromMicroseconds() - Creates a duration from a number of microseconds
- Time\Duration::fromMilliseconds() - Creates a duration from a number of milliseconds
- hrtime() - Get the system's high resolution time
来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。
