Time\Duration::fromSeconds

(PHP 8 >= 8.6.0)

Time\Duration::fromSeconds — Creates a duration from a number of seconds and nanoseconds

Descrizione

public static function Time\Duration::fromSeconds(int $seconds, int $nanoseconds = 0): Time\Duration

Creates a non-negative duration of seconds seconds plus nanoseconds nanoseconds.

Elenco dei parametri

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.

Valori restituiti

Returns a new Time\Duration object.

Errori/Eccezioni

Esempi

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;
}

Il precedente esempio visualizzerà:

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

Vedere anche:

+add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top