diff --git a/src/Duration.php b/src/Duration.php index 522ebbf..52607c4 100755 --- a/src/Duration.php +++ b/src/Duration.php @@ -157,11 +157,15 @@ public function toMinutes($duration = null, $roundToInteger = false) * Returns the duration as a colon formatted string * * For example, one hour and 42 minutes would be "1:43" + * With $zeroFill to true : + * - 42 minutes would be "0:42:00" + * - 28 seconds would be "0:00:28" * * @param int|string $duration A string or number, representing a duration + * @param bool $zeroFill A boolean, to force zero-fill result or not (see example) * @return string */ - public function formatted($duration = null) + public function formatted($duration = null, $zeroFill = false) { if (! is_null($duration)) { @@ -171,7 +175,7 @@ public function formatted($duration = null) $hours = $this->hours + ($this->days * $this->hoursPerDay); if ($this->seconds > 0) { - if ($this->seconds <= 9 && ($this->minutes > 0 || $hours > 0)) { + if ($this->seconds <= 9 && ($this->minutes > 0 || $hours > 0 || $zeroFill)) { $this->output .= '0' . $this->seconds; } else { $this->output .= $this->seconds; @@ -183,19 +187,23 @@ public function formatted($duration = null) } if ($this->minutes > 0) { - if ($this->minutes <= 9 && $hours > 0) { + if ($this->minutes <= 9 && ($hours > 0 || $zeroFill)) { $this->output = '0' . $this->minutes . ':' . $this->output; } else { $this->output = $this->minutes . ':' . $this->output; } } else { - if ($hours > 0) { + if ($hours > 0 || $zeroFill) { $this->output = '00' . ':' . $this->output; } } if ($hours > 0) { $this->output = $hours . ':' . $this->output; + } else { + if ($zeroFill) { + $this->output = '0' . ':' . $this->output; + } } return $this->output(); diff --git a/test/DurationTest.php b/test/DurationTest.php index 8658e66..4873796 100755 --- a/test/DurationTest.php +++ b/test/DurationTest.php @@ -119,6 +119,24 @@ public function testConvertingSecondsToFormattedString() $this->assertEquals('1:09:09', $this->d->formatted(4149)); } + public function testConvertingSecondsToFormattedStringZeroFilled() + { + $this->assertEquals('0:00:04', $this->d->formatted(4, true)); + $this->assertEquals('0:00:09', $this->d->formatted(9, true)); + $this->assertEquals('0:00:42', $this->d->formatted(42, true)); + $this->assertEquals('0:01:02', $this->d->formatted(62, true)); + $this->assertEquals('0:01:09', $this->d->formatted(69, true)); + $this->assertEquals('0:01:42', $this->d->formatted(102, true)); + $this->assertEquals('0:10:47', $this->d->formatted(647, true)); + $this->assertEquals('1:00:00', $this->d->formatted(3600, true)); + $this->assertEquals('1:00:01', $this->d->formatted(3601, true)); + $this->assertEquals('1:00:11', $this->d->formatted(3611, true)); + $this->assertEquals('1:01:00', $this->d->formatted(3660, true)); + $this->assertEquals('1:01:14', $this->d->formatted(3674, true)); + $this->assertEquals('1:04:25', $this->d->formatted(3865, true)); + $this->assertEquals('1:09:09', $this->d->formatted(4149, true)); + } + public function testConvertingFormattedStringsToSeconds() { $this->assertEquals(4, $this->d->toSeconds('4'));