Core\Time\Duration::toMicroseconds
$duration->toMicroseconds(): intReturns the length of a duration as a number of whole microseconds. A microsecond is one millionth of a second, or 1000 nanoseconds.
Use it when another tool expects microseconds, for example a database log that shows how long each query took. The method counts only whole microseconds. The part that is shorter than one microsecond is ignored, so 1500 nanoseconds gives 1. A negative duration gives a negative number in the same way: -1500 nanoseconds gives -1. Every duration fits, so this method never throws.
Use toNanoseconds when you need the exact length.
Return value
Returns int. The nanosecond count divided by 1000, truncated toward zero.
Examples
Read whole microseconds
<?nvs// Shows how to read the length of a duration as a number of microseconds.
Core\Time\Duration $wait = 2ms;// 2 milliseconds is 2000 microseconds. This prints 2000.echo $wait->toMicroseconds(), "\n";
// A negative duration gives a negative number. This prints -75.Core\Time\Duration $back = 75us;echo $back->negated()->toMicroseconds(), "\n";2000-75Only whole microseconds
<?nvs// Shows that `toMicroseconds` counts only whole microseconds.
// 1999 nanoseconds is 1 whole microsecond and 999 nanoseconds more. This prints 1.echo Core\Time\Duration::nanoseconds(1999)->toMicroseconds(), "\n";
// The same is true below zero. This prints -1.echo Core\Time\Duration::nanoseconds(-1999)->toMicroseconds(), "\n";
// A duration shorter than one microsecond gives 0. This prints 0.echo Core\Time\Duration::nanoseconds(999)->toMicroseconds(), "\n";1-10Log slow queries
<?nvs// Writes the time of each database query in microseconds, and marks the slow ones.
array<Core\Time\Duration> $timings = [450us, 1200500ns, 87us, 3ms];Core\Time\Duration $limit = 1ms;
// This prints 450, 1200 slow, 87 and 3000 slow, one per line.foreach ($timings as Core\Time\Duration $took) { echo $took->toMicroseconds(); if ($took->compareTo($limit) > 0) { echo ' slow'; } echo "\n";}4501200 slow873000 slow