Turn a number of seconds into hours, minutes and seconds with whole-number division.
What to build
A program that prints a duration as H:MM:SS.
Requirements
- Read the number of seconds.
- Print it as
H:MM:SSwhere the hours have no leading zero and the minutes and seconds always have two digits. Hours may be larger than 23.
Input
One line: the number of seconds.
Output
One line.
Constraints
0 ≤ seconds ≤ 1000000.
Example
Input:
3661
Output:
1:01:01
Why it works
intdiv and the remainder operator split the total, and %02d pads with a zero.
Examples
Example 1 · Input
3661
Expected output
1:01:01
Example 2 · Input
59
Expected output
0:00:59
Hints
Hint 1
Hours are intdiv($s, 3600); the minutes are intdiv($s % 3600, 60); the seconds are $s % 60.
Hint 2
sprintf("%d:%02d:%02d") pads minutes and seconds with a zero.
Solution
Show one correct solution (try it yourself first)
<?php
$s = (int) trim(fgets(STDIN));
$h = intdiv($s, 3600);
$m = intdiv($s % 3600, 60);
$sec = $s % 60;
echo sprintf("%d:%02d:%02d", $h, $m, $sec), "\n";