Core\Time\Duration::fromClock
Core\Time\Duration::fromClock(string $text): Core\Time\DurationCore\Time\Duration::fromClock( string $text, ): Core\Time\Duration
Reads a duration from text in hours and minutes, such as 1:30, or in hours, minutes and seconds, such as 1:30:15. Use it for a time a person types into a form.
The hours may have any number of digits, so 25:30 is 25 hours and 30 minutes. The minutes and the seconds have two digits each. A - at the start makes the duration negative, such as -1:15.
fromClock throws a ParseError in these cases:
- The text is not in this form. Spaces and a
+sign are not allowed. - The minutes or the seconds are larger than
59. - The duration is longer than
2562047:47:16, the longestDuration.
fromClock can read every text that Core\Time\Duration::toClock returns. To read 1h30m, the syntax you use in code, call Core\Time\Duration::parse.
The examples read a few times, show which texts throw an error, and check the times a person types into a form.
Parameters
$textstringThe text starts with the hours, which may have any number of digits. Next come:and the minutes as two digits, such as05. The seconds are optional, also as:and two digits. A-at the start makes the duration negative.
Return value
Returns Core\Time\Duration. A new Duration. Duration::fromClock('25:30') is 25 hours and 30 minutes.
Errors
ParseError$textis not in this form, or the minutes or the seconds are larger than59.ParseErroris also thrown above2562047:47:16, the longestDuration.
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
Reading hours and minutes
<?nvs// Reads durations written as hours and minutes, or as hours, minutes and seconds.
// `1:30` is one hour and 30 minutes. This prints 1h30m.echo Core\Time\Duration::fromClock('1:30'), "\n";
// The hours may be more than 24. This prints 90000, the number of seconds.echo Core\Time\Duration::fromClock('25:00')->toSeconds(), "\n";
// A third part is the seconds. This prints 1h30m15s.echo Core\Time\Duration::fromClock('1:30:15'), "\n";
// A `-` at the start makes the duration negative. This prints -45m.echo Core\Time\Duration::fromClock('-0:45'), "\n";1h30m900001h30m15s-45mText that is not a clock time
<?nvs// Shows which texts `fromClock` throws a `ParseError` for.
// The minutes and the seconds must be two digits from `00` to `59`.// Only `1:30` is read, and the other texts throw. `fromClock` does not read `1h30m`, the syntax you use in code.array<string> $texts = ['1:60', '1:5', '1:30:75', '1h30m', '1:30', ''];foreach ($texts as string $text) { try { Core\Time\Duration $read = Core\Time\Duration::fromClock($text); echo "'", $text, "' is ", $read, "\n"; } catch (ParseError $e) { echo "'", $text, "' throws a ParseError\n"; }}'1:60' throws a ParseError'1:5' throws a ParseError'1:30:75' throws a ParseError'1h30m' throws a ParseError'1:30' is 1h30m'' throws a ParseErrorBreak time from a form
<?nvs// Reads the hours worked and the break from a form, and checks the input.
class Timesheet{ // `net` returns the time worked minus the break. If a text is not a clock time, // `net` returns an error message for the form. public static function net(string $worked, string $break): string { try { Core\Time\Duration $total = Core\Time\Duration::fromClock($worked); Core\Time\Duration $pause = Core\Time\Duration::fromClock($break); return 'worked ' . $total->minus($pause)->toClock(); } catch (ParseError $e) { return 'please write the time as hours and minutes, such as 1:30'; } }}
// 8:45 minus a 0:30 break. This prints "worked 8:15".echo Timesheet::net('8:45', '0:30'), "\n";
// 0:90 has more than 59 minutes, so the form shows the error message.echo Timesheet::net('8:45', '0:90'), "\n";worked 8:15please write the time as hours and minutes, such as 1:30