Skip to content

Commit

Permalink
Merge pull request #5 from dimitri-koenig/feature/toMinutes
Browse files Browse the repository at this point in the history
Added toMinutes() method #4
  • Loading branch information
kevinkhill authored Sep 10, 2016
2 parents bae9a28 + 7b0d603 commit 4b1fe10
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ The library can accept either in colon separated format, like 2:43 for 2 minutes
OR
written as human readable or abbreviated time, such as 6m21s for 6 minutes and 21 seconds.

Both can be converted into seconds for easy storage into a database.
Both can be converted into seconds and minutes for easy storage into a database.

Seconds, colon separated, abbreviated, all three can be parsed and interchanged.
- supports hours, minutes, and seconds
Expand All @@ -39,6 +39,8 @@ $duration = new Duration('7:31');
echo $duration->humanize(); // 7m 31s
echo $duration->formatted(); // 7:31
echo $duration->toSeconds(); // 451
echo $duration->toMinutes(); // 7.5166
echo $duration->toMinutes(null, true); // 8
```

```php
Expand All @@ -47,6 +49,8 @@ $duration = new Duration('1h 2m 5s');
echo $duration->humanize(); // 1h 2m 5s
echo $duration->formatted(); // 1:02:05
echo $duration->toSeconds(); // 3725
echo $duration->toMinutes(); // 62.0833
echo $duration->toMinutes(null, true); // 62
```

```php
Expand All @@ -55,6 +59,8 @@ $duration = new Duration('4293');
echo $duration->humanize(); // 1h 11m 33s
echo $duration->formatted(); // 1:11:33
echo $duration->toSeconds(); // 4293
echo $duration->toMinutes(); // 71.55
echo $duration->toMinutes(null, true); // 72
```

# Note
Expand All @@ -65,4 +71,6 @@ $duration = new Duration;
echo $duration->humanize('1h 2m 5s'); // 1h 2m 5s
echo $duration->formatted('1h 2m 5s'); // 1:02:05
echo $duration->toSeconds('1h 2m 5s'); // 3725
echo $duration->toMinutes('1h 2m 5s'); // 62.0833
echo $duration->toMinutes('1h 2m 5s', true); // 62
```
23 changes: 22 additions & 1 deletion src/Duration.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function parse($duration)
/**
* Returns the duration as an amount of seconds.
*
* For example, one hour and 42 minutes would be "102"
* For example, one hour and 42 minutes would be "6120"
*
* @param int|string $duration A string or number, representing a duration
* @return int
Expand All @@ -112,6 +112,27 @@ public function toSeconds($duration = null)
return (int) $this->output();
}

/**
* Returns the duration as an amount of seconds.
*
* For example, one hour and 42 minutes would be "102" minutes
*
* @param int|string $duration A string or number, representing a duration
* @param boolean $roundToInteger Should the number be rounded and returned as integer
* @return int
*/
public function toMinutes($duration = null, $roundToInteger = false)
{
if (! is_null($duration)) {
$this->parse($duration);
}

$this->output = ($this->hours * 60 * 60) + ($this->minutes * 60) + $this->seconds;
$result = intval($this->output()) / 60;

return $roundToInteger ? intval(round($result, 0)) : $result;
}

/**
* Returns the duration as a colon formatted string
*
Expand Down
28 changes: 28 additions & 0 deletions test/DurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,34 @@ public function testConvertingFormattedStringsToSeconds()
$this->assertEquals(3865, $this->d->toSeconds('1:04:25'));
}

public function testConvertingFormattedStringsToMinutes()
{
$this->assertEquals(4/60, $this->d->toMinutes('4'));
$this->assertEquals(42/60, $this->d->toMinutes('42'));
$this->assertEquals(62/60, $this->d->toMinutes('1:02'));
$this->assertEquals(102/60, $this->d->toMinutes('1:42'));
$this->assertEquals(647/60, $this->d->toMinutes('10:47'));
$this->assertEquals(3600/60, $this->d->toMinutes('1:00:00'));
$this->assertEquals(3601/60, $this->d->toMinutes('1:00:01'));
$this->assertEquals(3611/60, $this->d->toMinutes('1:00:11'));
$this->assertEquals(3660/60, $this->d->toMinutes('1:01:00'));
$this->assertEquals(3674/60, $this->d->toMinutes('1:01:14'));
$this->assertEquals(3865/60, $this->d->toMinutes('1:04:25'));

$this->assertEquals(0, $this->d->toMinutes('4', true));
$this->assertEquals(1, $this->d->toMinutes('42', true));
$this->assertEquals(1, $this->d->toMinutes('1:02', true));
$this->assertEquals(2, $this->d->toMinutes('1:42', true));
$this->assertEquals(11, $this->d->toMinutes('10:47', true));
$this->assertEquals(60, $this->d->toMinutes('1:00:00', true));
$this->assertEquals(60, $this->d->toMinutes('1:00:01', true));
$this->assertEquals(60, $this->d->toMinutes('1:00:11', true));
$this->assertEquals(61, $this->d->toMinutes('1:01:00', true));
$this->assertEquals(61, $this->d->toMinutes('1:01:14', true));
$this->assertEquals(64, $this->d->toMinutes('1:04:25', true));
$this->assertEquals(65, $this->d->toMinutes('1:04:55', true));
}

public function testConvertSecondsToHumanizedString()
{
$this->assertEquals('4s', $this->d->humanize(4));
Expand Down

0 comments on commit 4b1fe10

Please sign in to comment.