Skip to content

Commit 6602871

Browse files
committed
Added SecondsToTime string macro
1 parent 8071c87 commit 6602871

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to `laravel-mixins` will be documented in this file.
44

5+
## 3.3.0 - 2022-02-22
6+
7+
- Added `SecondsToTime` string macro.
8+
59
## 3.2.0 - 2022-02-04
610

711
- Added supprt for Laravel 9

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ There's no Service Provider or automatic discovery/registration of anything. All
4848
* [Human Filesize](#human-filesize)
4949
* [Text](#text)
5050
* [URL](#url)
51+
* [Seconds to time][#seconds-to-time]
5152

5253
#### PDF
5354
* [PDF Regeneration](#pdf-regeneration)
@@ -271,6 +272,21 @@ $url = "protone.media";
271272
Str::url($url);
272273
```
273274

275+
### Seconds to time
276+
277+
Converts seconds to a 'mm:ss' / 'hh:mm:ss' format.
278+
279+
```php
280+
Str::mixin(new ProtoneMedia\LaravelMixins\String\SecondsToTime);
281+
282+
Str::secondsToTime(10); // 00:10
283+
Str::secondsToTime(580); // 09:40
284+
Str::secondsToTime(3610); // 01:00:10
285+
286+
// force 'hh:mm:ss' format, even under an hour:
287+
Str::secondsToTime(580, false); // 00:09:40
288+
```
289+
274290
## PDF Regeneration
275291

276292
*Note: Requires the `symfony/process` package.*

src/String/SecondsToTime.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace ProtoneMedia\LaravelMixins\String;
4+
5+
class SecondsToTime
6+
{
7+
/**
8+
* Returns a function containing the macro.
9+
*
10+
* @return callable
11+
*/
12+
public function secondsToTime(): callable
13+
{
14+
/**
15+
* Converts seconds to 'mm:ss' / 'hh:mm:ss' format.
16+
*/
17+
return function (int $seconds, bool $omitHours = true): string {
18+
if (!$omitHours || $seconds >= 3600) {
19+
return sprintf('%02d:%02d:%02d', ($seconds / 3600), ($seconds / 60 % 60), $seconds % 60);
20+
}
21+
22+
return sprintf('%02d:%02d', ($seconds / 60 % 60), $seconds % 60);
23+
};
24+
}
25+
}

tests/String/SecondsToTimeTest.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace ProtoneMedia\Mixins\Tests\String;
4+
5+
use Illuminate\Support\Str;
6+
use Orchestra\Testbench\TestCase;
7+
use ProtoneMedia\LaravelMixins\String\SecondsToTime;
8+
9+
class SecondsToTimeTest extends TestCase
10+
{
11+
protected function setUp(): void
12+
{
13+
parent::setUp();
14+
15+
Str::mixin(new SecondsToTime);
16+
}
17+
18+
/** @test */
19+
public function it_can_format_seconds_as_time()
20+
{
21+
$this->assertEquals('01:00:10', Str::secondsToTime(3610));
22+
$this->assertEquals('09:40', Str::secondsToTime(580));
23+
$this->assertEquals('00:01', Str::secondsToTime(1));
24+
$this->assertEquals('00:00', Str::secondsToTime(0));
25+
$this->assertEquals('00:09:40', Str::secondsToTime(580, false));
26+
}
27+
}

0 commit comments

Comments
 (0)