Skip to content

Commit

Permalink
#7 Adding zerofill feature in formatted() function
Browse files Browse the repository at this point in the history
  • Loading branch information
Ydalb committed Apr 7, 2018
1 parent 80b89d9 commit 90e099a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/Duration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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;
Expand All @@ -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();
Expand Down
18 changes: 18 additions & 0 deletions test/DurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down

0 comments on commit 90e099a

Please sign in to comment.