Skip to content

Core\Time\Duration::minutes

Creates a duration of a given number of minutes. Each minute is exactly 60 seconds.

Use it when the number of minutes is known only while the program runs, for example when the length of a meeting or a session timeout comes from a setting. When the number is fixed, write the duration directly in the code, such as 15m. A negative number gives a negative duration. The number can be up to 153,722,867 minutes, which is about 292 years. A larger number throws a RuntimeError.

toString shows the duration in the largest units that fit. So 90 minutes prints as 1h30m.

Parameters

  • $nintThe count of minutes; negative for a duration that runs backwards.

Return value

Returns Core\Time\Duration. A Duration of $n × 60 seconds.

Errors

  • RuntimeError$n minutes is longer than a Duration can hold — about 292 years.

Examples

From a computed number

<?nvs
// Shows how to create a duration from a number of minutes that the program computes.
int $lessons = 2;
Core\Time\Duration $course = Core\Time\Duration::minutes($lessons * 45);
// 90 minutes is 1 hour and 30 minutes. This prints 1h30m.
echo $course->toString(), "\n";
// A minute is 60 seconds, so this prints 5400.
echo $course->toSeconds(), "\n";
// A negative number gives a negative duration. This prints -15m.
echo Core\Time\Duration::minutes(-15)->toString(), "\n";
Output
1h30m
5400
-15m

Too many minutes

<?nvs
// Shows what happens when the number of minutes is too large for a duration.
// 153722867 minutes is the largest number that fits. This prints 153722867.
echo Core\Time\Duration::minutes(153722867)->toSeconds() / 60, "\n";
// One more minute does not fit, so `minutes` throws a `RuntimeError`.
try {
Core\Time\Duration::minutes(153722868);
} catch (RuntimeError $e) {
echo $e->message, "\n";
}
Output
153722867
Core\Time\Duration::minutes(): this duration is longer than `Core\Time\Duration` can hold

When each meeting ends

<?nvs
// Computes when each meeting ends, from its start time and its length in minutes.
array<string> $starts = ['2024-06-10T09:00:00Z', '2024-06-10T11:30:00Z', '2024-06-10T15:45:00Z'];
array<int> $lengths = [45, 90, 20];
// The meetings end at 09:45, 13:00 and 16:05. This prints each end time in full.
int $i = 0;
while ($i < 3) {
var $start = Core\Time\Instant::fromIso($starts[$i]);
Core\Time\Duration $length = Core\Time\Duration::minutes($lengths[$i]);
echo $start->plus($length)->toIso(), "\n";
$i = $i + 1;
}
Output
2024-06-10T09:45:00Z
2024-06-10T13:00:00Z
2024-06-10T16:05:00Z