php8.5
Home/ Manual/ time / duration/ Time\Duration::fromHours

Time\Duration::fromHours

PHP function Edit on GitHub ✎

(PHP 8 >= 8.6.0)

Creates a duration from a number of hours

Description

Time\Duration::fromHours(int $hours): Time\Duration

Creates a non-negative duration of hours hours. The value is normalized into whole seconds and nanoseconds, see the Time\Duration::$seconds and Time\Duration::$nanoseconds properties.

Parameters

hours

The number of hours. Must be greater than or equal to 0.

Return Values

Returns a new Time\Duration object.

Errors/Exceptions

  • If hours is negative, a ValueError is thrown.

Examples

Time\Duration::fromHours example

php
<?php
var_dump(\Time\Duration::fromHours(24));

try {
    \Time\Duration::fromHours(PHP_INT_MAX);
} catch (\Time\TimeException $e) {
    echo $e->getMessage(), PHP_EOL;
}

The above example will output:

output
object(Time\Duration)#1 (3) {
  ["seconds"]=>
  int(86400)
  ["nanoseconds"]=>
  int(0)
  ["negative"]=>
  bool(false)
}
The maximum representable range is 9_223_372_035 seconds (roughly 292 years)

See Also

  • Time\Duration::fromSeconds
  • Time\Duration::fromMinutes
  • Time\Duration::fromIso8601DurationString

Source: reference/datetime/time/duration/fromhours.xml · from the official PHP manual (php/doc-en)