Core\Time\Duration::weeks
Core\Time\Duration::weeks(int $n): Core\Time\DurationCore\Time\Duration::weeks( int $n, ): Core\Time\Duration
Creates a duration of a given number of weeks. Each week is exactly 7 days of 24 hours, which is 168 hours.
Use it when the number of weeks 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 2w. A negative number gives a negative duration. The number can be up to 15,250 weeks, which is about 292 years. A larger number throws a RuntimeError.
Good to know: a calendar week is not always 168 hours. In a week where the clocks change for daylight saving time, it has one hour more or less. To move a date to the same time one calendar week later, use plus on a Core\Time\DateTime with Core\Unit::Week.
Parameters
$nintThe count of 168-hour weeks; negative for a duration that runs backwards.
Return value
Returns Core\Time\Duration. A Duration of $n × 604 800 seconds.
Errors
RuntimeError$nweeks is longer than aDurationcan hold — about 292 years.
Examples
From a computed number
<?nvs// Shows how to create a duration from a number of weeks that the program computes.
// A project has 6 sprints, and each sprint is 2 weeks long.int $sprints = 6;Core\Time\Duration $project = Core\Time\Duration::weeks($sprints * 2);
// This prints 12w.echo $project->toString(), "\n";
// A week is 604800 seconds, so this prints 7257600.echo $project->toSeconds(), "\n";
// The same length written as days is the same duration. This prints "same".echo $project->compareTo(Core\Time\Duration::days(84)) == 0 ? 'same' : 'different', "\n";12w7257600sameToo many weeks
<?nvs// Shows the largest number of weeks a duration can have, and what happens past it.
// 15250 weeks is about 292 years. It fits, so this prints 15250w.echo Core\Time\Duration::weeks(15250)->toString(), "\n";
// One more week does not fit, so `weeks` throws a `RuntimeError`. This prints "too long".try { Core\Time\Duration::weeks(15251);} catch (RuntimeError $e) { echo "too long\n";}
// A negative number gives a negative duration. This prints -2w.echo Core\Time\Duration::weeks(-2)->toString(), "\n";15250wtoo long-2wWhen a trial ends
<?nvs// Works out when a free trial ends, from a number of weeks in the settings.
array<int> $settings = ['trialWeeks' => 2];Core\Time\Duration $trial = Core\Time\Duration::weeks($settings['trialWeeks']);
var $signedUp = Core\Time\Instant::fromIso('2024-05-01T09:00:00Z');var $ends = $signedUp->plus($trial);
// This prints 2024-05-15 09:00.echo $ends->in('UTC')->format('yyyy-MM-dd HH:mm'), "\n";
// A visit 15 days after sign-up is after the end, so this prints "trial over".var $visit = $signedUp->plus(15d);echo $visit < $ends ? 'trial active' : 'trial over', "\n";2024-05-15 09:00trial over