Core\Time\Duration::toClock
Returns a duration as text in hours and minutes, such as 1:30. A timesheet shows hours this way.
The hours do not stop at 24, so 25 hours and 30 minutes gives 25:30. Five minutes gives 0:05. A negative duration starts with -, such as -1:15. The method never throws.
Pass { seconds: true } to add the seconds, such as 25:30:00. By default, the text ends at the minutes. The seconds are cut off, not rounded, so 119 seconds gives 0:01. A negative duration that is cut off to zero gives 0:00.
Core\Time\Duration::fromClock reads this text and returns a duration.
The examples show a few durations as text, the seconds option with a negative duration, and the hours of a weekly timesheet.
Good to know: toString writes the same duration as 1h30m, the way you write it in code.
Options
secondsbooloptionalWith{ seconds: true }, the text also has the seconds, such as25:30:00. By default, the text ends at the minutes.
Return value
Returns string. The text. The seconds are dropped when the text ends at the minutes, and the part below a second is always dropped. Nothing is rounded, so 1 hour, 30 minutes and 59 seconds is 1:30. A negative duration starts with -, such as -1:15. Duration::fromClock reads the text back.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Time\Duration::toClock writes a duration as hours and minutes, such as 25:30 or -1:15. With seconds: true it also writes the seconds. Core\Time\Duration::fromClock reads such a text back to a Duration. |
Examples
Hours and minutes
<?nvs// Shows how a duration is written as hours and minutes.
// 90 minutes is one hour and 30 minutes. This prints 1:30.echo (90m)->toClock(), "\n";
// The minutes always have two digits. This prints 0:05.echo (5m)->toClock(), "\n";
// The hours do not stop at 24. This prints 25:30.echo (1d)->plus(1h30m)->toClock(), "\n";
// The seconds are cut off, not rounded. This prints 0:01.echo (119s)->toClock(), "\n";1:300:0525:300:01Seconds and negative durations
<?nvs// Shows the `seconds` option and a negative duration.
// With `seconds: true`, the text ends with the seconds. This prints 25:30:00.echo (25h30m)->toClock({ seconds: true }), "\n";
// This prints 0:01:59.echo (119s)->toClock({ seconds: true }), "\n";
// A negative duration starts with `-`. This prints -1:15.Core\Time\Duration $behind = (30m)->minus(1h45m);echo $behind->toClock(), "\n";
// `Duration::fromClock` reads the text back. This prints "same".string $text = $behind->toClock();echo Core\Time\Duration::fromClock($text)->compareTo($behind) == 0 ? 'same' : 'different', "\n";25:30:000:01:59-1:15sameHours worked in a week
<?nvs// Adds up the hours on a weekly timesheet and prints each day and the total.
array<string> $days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'];array<Core\Time\Duration> $worked = [8h15m, 7h45m, 9h, 8h30m, 6h50m];
Core\Time\Duration $total = 0s;int $i = 0;while ($i < 5) { // Each day prints as hours and minutes, such as "Mon 8:15". echo $days[$i], ' ', $worked[$i]->toClock(), "\n"; $total = $total->plus($worked[$i]); $i = $i + 1;}
// The total is more than 24 hours. This prints "total 40:20".echo 'total ', $total->toClock(), "\n";
// `minus(40h)` gives the time worked past a 40-hour week. This prints "overtime 0:20".echo 'overtime ', $total->minus(40h)->toClock(), "\n";Mon 8:15Tue 7:45Wed 9:00Thu 8:30Fri 6:50total 40:20overtime 0:20