Core\Time\Duration::hours
Core\Time\Duration::hours(int $n): Core\Time\DurationCore\Time\Duration::hours( int $n, ): Core\Time\Duration
Creates a duration of a given number of hours. Each hour is exactly 3600 seconds.
Use it when the number of hours is known only while the program runs, for example when it comes from a setting. When the number is fixed, write the duration directly in the code, such as 72h. A negative number gives a negative duration. The number can be up to 2,562,047 hours, which is about 292 years. A larger number throws a RuntimeError.
Good to know: adding 72h to a time is not always the same as adding three calendar days. On the day the clocks change for daylight saving time, the clock shows a different hour afterwards.
Parameters
$nintThe count of hours; negative for a duration that runs backwards.
Return value
Returns Core\Time\Duration. A Duration of $n × 3600 seconds.
Errors
RuntimeError$nhours is longer than aDurationcan hold — about 292 years.
Examples
From a computed number
<?nvs// Shows how to create a duration from a number of hours that the program computes.
int $shifts = 3;Core\Time\Duration $work = Core\Time\Duration::hours($shifts * 8);
// 24 hours is written as one day, so this prints 1d.echo $work->toString(), "\n";
// An hour is 3600 seconds, so this prints 86400.echo $work->toSeconds(), "\n";
// A negative number gives a negative duration. This prints -5h.echo Core\Time\Duration::hours(-5)->toString(), "\n";1d86400-5hToo many hours
<?nvs// Shows what happens when the number of hours is too large for a duration.
// 2562047 hours is the largest number that fits. This prints 2562047.echo Core\Time\Duration::hours(2562047)->toSeconds() / 3600, "\n";
// One more hour does not fit, so `hours` throws a `RuntimeError`.try { Core\Time\Duration::hours(2562048);} catch (RuntimeError $e) { echo $e->message, "\n";}2562047Core\Time\Duration::hours(): this duration is longer than `Core\Time\Duration` can holdA download link that expires
<?nvs// Checks whether a download link is still valid, from a number of hours in the settings.
array<int> $settings = ['linkHours' => 48];Core\Time\Duration $valid = Core\Time\Duration::hours($settings['linkHours']);
var $created = Core\Time\Instant::fromIso('2024-06-10T15:30:00Z');array<string> $clicks = ['2024-06-11T08:00:00Z', '2024-06-12T15:29:59Z', '2024-06-12T15:30:00Z'];
// The link works until 48 hours after it was created. This prints "ok", "ok" and "expired".foreach ($clicks as string $at) { echo Core\Time\Instant::fromIso($at) < $created->plus($valid) ? 'ok' : 'expired', "\n";}okokexpired