Core\Time\Duration::days
Core\Time\Duration::days(int $n): Core\Time\DurationCore\Time\Duration::days( int $n, ): Core\Time\Duration
Creates a duration of a given number of days. Each day is exactly 24 hours.
Use it when the number of days 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 30d. A negative number gives a negative duration. The number can be up to 106,751 days, which is about 292 years. A larger number throws a RuntimeError.
Good to know: a calendar day is not always 24 hours. On the day the clocks change for daylight saving time, it has 23 or 25 hours. To move a date to the same time on the next calendar day, use plus on a Core\Time\DateTime with Core\Unit::Day.
Parameters
$nintThe count of 24-hour days; negative for a duration that runs backwards.
Return value
Returns Core\Time\Duration. A Duration of $n × 86 400 seconds.
Errors
RuntimeError$ndays is longer than aDurationcan hold — about 292 years.
Examples
From a computed number
<?nvs// Shows how to create a duration from a number of days that the program computes.
int $weeks = 2;Core\Time\Duration $holiday = Core\Time\Duration::days($weeks * 7);
// 14 days are written as two weeks, so this prints 2w.echo $holiday->toString(), "\n";
// A day is 86400 seconds, so this prints 1209600.echo $holiday->toSeconds(), "\n";
// A negative number gives a negative duration. This prints -3d.echo Core\Time\Duration::days(-3)->toString(), "\n";2w1209600-3dA day is always 24 hours
<?nvs// Shows that a day from `days` is always 24 hours, also when the clocks change.
var $berlin = Core\Time\Zone::of('Europe/Berlin');// Noon on the day before Berlin moves its clocks forward by one hour.var $noon = Core\Time\DateTime::fromIso('2024-03-30 12:00', $berlin);
// 24 hours later, the clock in Berlin shows 13:00.echo $noon->toInstant()->plus(Core\Time\Duration::days(1))->in($berlin)->format('HH:mm'), "\n";
// One calendar day later, the clock shows 12:00 again.echo $noon->plus(1, Core\Unit::Day)->format('HH:mm'), "\n";13:0012:00When a login expires
<?nvs// Works out when a login expires, from a number of days in the settings.
array<int> $settings = ['rememberDays' => 30];Core\Time\Duration $remember = Core\Time\Duration::days($settings['rememberDays']);
var $loggedIn = Core\Time\Instant::fromIso('2024-05-01T09:00:00Z');var $expires = $loggedIn->plus($remember);
// This prints 2024-05-31 09:00.echo $expires->in('UTC')->format('yyyy-MM-dd HH:mm'), "\n";
// A visit 29 days later is still inside the time, so this prints "still logged in".var $visit = $loggedIn->plus(29d);echo $visit < $expires ? 'still logged in' : 'expired', "\n";2024-05-31 09:00still logged in