Skip to content

Commit 4b1fe10

Browse files
authored
Merge pull request #5 from dimitri-koenig/feature/toMinutes
Added toMinutes() method #4
2 parents bae9a28 + 7b0d603 commit 4b1fe10

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The library can accept either in colon separated format, like 2:43 for 2 minutes
1515
OR
1616
written as human readable or abbreviated time, such as 6m21s for 6 minutes and 21 seconds.
1717

18-
Both can be converted into seconds for easy storage into a database.
18+
Both can be converted into seconds and minutes for easy storage into a database.
1919

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

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

5256
```php
@@ -55,6 +59,8 @@ $duration = new Duration('4293');
5559
echo $duration->humanize(); // 1h 11m 33s
5660
echo $duration->formatted(); // 1:11:33
5761
echo $duration->toSeconds(); // 4293
62+
echo $duration->toMinutes(); // 71.55
63+
echo $duration->toMinutes(null, true); // 72
5864
```
5965

6066
# Note
@@ -65,4 +71,6 @@ $duration = new Duration;
6571
echo $duration->humanize('1h 2m 5s'); // 1h 2m 5s
6672
echo $duration->formatted('1h 2m 5s'); // 1:02:05
6773
echo $duration->toSeconds('1h 2m 5s'); // 3725
74+
echo $duration->toMinutes('1h 2m 5s'); // 62.0833
75+
echo $duration->toMinutes('1h 2m 5s', true); // 62
6876
```

src/Duration.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function parse($duration)
9696
/**
9797
* Returns the duration as an amount of seconds.
9898
*
99-
* For example, one hour and 42 minutes would be "102"
99+
* For example, one hour and 42 minutes would be "6120"
100100
*
101101
* @param int|string $duration A string or number, representing a duration
102102
* @return int
@@ -112,6 +112,27 @@ public function toSeconds($duration = null)
112112
return (int) $this->output();
113113
}
114114

115+
/**
116+
* Returns the duration as an amount of seconds.
117+
*
118+
* For example, one hour and 42 minutes would be "102" minutes
119+
*
120+
* @param int|string $duration A string or number, representing a duration
121+
* @param boolean $roundToInteger Should the number be rounded and returned as integer
122+
* @return int
123+
*/
124+
public function toMinutes($duration = null, $roundToInteger = false)
125+
{
126+
if (! is_null($duration)) {
127+
$this->parse($duration);
128+
}
129+
130+
$this->output = ($this->hours * 60 * 60) + ($this->minutes * 60) + $this->seconds;
131+
$result = intval($this->output()) / 60;
132+
133+
return $roundToInteger ? intval(round($result, 0)) : $result;
134+
}
135+
115136
/**
116137
* Returns the duration as a colon formatted string
117138
*

test/DurationTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,34 @@ public function testConvertingFormattedStringsToSeconds()
107107
$this->assertEquals(3865, $this->d->toSeconds('1:04:25'));
108108
}
109109

110+
public function testConvertingFormattedStringsToMinutes()
111+
{
112+
$this->assertEquals(4/60, $this->d->toMinutes('4'));
113+
$this->assertEquals(42/60, $this->d->toMinutes('42'));
114+
$this->assertEquals(62/60, $this->d->toMinutes('1:02'));
115+
$this->assertEquals(102/60, $this->d->toMinutes('1:42'));
116+
$this->assertEquals(647/60, $this->d->toMinutes('10:47'));
117+
$this->assertEquals(3600/60, $this->d->toMinutes('1:00:00'));
118+
$this->assertEquals(3601/60, $this->d->toMinutes('1:00:01'));
119+
$this->assertEquals(3611/60, $this->d->toMinutes('1:00:11'));
120+
$this->assertEquals(3660/60, $this->d->toMinutes('1:01:00'));
121+
$this->assertEquals(3674/60, $this->d->toMinutes('1:01:14'));
122+
$this->assertEquals(3865/60, $this->d->toMinutes('1:04:25'));
123+
124+
$this->assertEquals(0, $this->d->toMinutes('4', true));
125+
$this->assertEquals(1, $this->d->toMinutes('42', true));
126+
$this->assertEquals(1, $this->d->toMinutes('1:02', true));
127+
$this->assertEquals(2, $this->d->toMinutes('1:42', true));
128+
$this->assertEquals(11, $this->d->toMinutes('10:47', true));
129+
$this->assertEquals(60, $this->d->toMinutes('1:00:00', true));
130+
$this->assertEquals(60, $this->d->toMinutes('1:00:01', true));
131+
$this->assertEquals(60, $this->d->toMinutes('1:00:11', true));
132+
$this->assertEquals(61, $this->d->toMinutes('1:01:00', true));
133+
$this->assertEquals(61, $this->d->toMinutes('1:01:14', true));
134+
$this->assertEquals(64, $this->d->toMinutes('1:04:25', true));
135+
$this->assertEquals(65, $this->d->toMinutes('1:04:55', true));
136+
}
137+
110138
public function testConvertSecondsToHumanizedString()
111139
{
112140
$this->assertEquals('4s', $this->d->humanize(4));

0 commit comments

Comments
 (0)