An open API service indexing awesome lists of open source software.

https://github.com/jeromegamez/duration-php

Working with durations made easy
https://github.com/jeromegamez/duration-php

duration php time

Last synced: 9 months ago
JSON representation

Working with durations made easy

Awesome Lists containing this project

README

          

# Duration for PHP

Working with durations made easy.

[![Current version](https://img.shields.io/packagist/v/gamez/duration.svg)](https://packagist.org/packages/gamez/duration)
![Supported PHP version](https://img.shields.io/packagist/php-v/gamez/duration.svg)
[![Code Coverage](https://codecov.io/gh/jeromegamez/duration-php/branch/master/graph/badge.svg)](https://codecov.io/gh/jeromegamez/duration-php)
[![Sponsor](https://img.shields.io/static/v1?logo=GitHub&label=Sponsor&message=%E2%9D%A4&color=ff69b4)](https://github.com/sponsors/jeromegamez)

Do you like to use `DateInterval` to compute and work with durations? Me neither, so let's fix that!

* [Installation](#installation)
* [Reference](#reference)
* [Supported Input Values](#supported-input-values)
* [Transformations](#transformations)
* [Comparisons](#comparisons)
* [Operations](#operations)
* [Roadmap](#roadmap)

---

## Installation

You can install the package with [Composer](https://getcomposer.org):

```bash
composer require gamez/duration
```

You can then use Duration:

```php
isSmallerThan($oneMinute); // true
$oneHour->isLargerThan($oneMinute); // true
$oneMinute->equals($sixtySeconds); // true

$durations = [$oneMinute, $oneSecond, $oneHour, $sixtySeconds];

usort($durations, function ($a, $b) {
return $a->compareTo($b);
}); // -> [$oneSecond, $sixtySeconds, $oneMinute, $oneHour]
```

### Operations

Results will always be rounded by the second.

```php
use Gamez\Duration;

$thirty = Duration::make('30 seconds');

echo $thirty->withAdded('31 seconds'); // PT1M1S
echo $thirty->withSubtracted('29 seconds'); // PT1S
echo $thirty->multipliedBy(3); // PT1M30S
echo $thirty->dividedBy(2.5); // PT12S

$thirty->multipliedBy(-1); // InvalidArgumentException
$thirty->withSubtracted('31 seconds'); // InvalidArgumentException
```

---

## Roadmap

* Support more input formats
* Add "output for humans" (like colon notation)
* Support precision (similar to [spatie/period](https://github.com/spatie/period))
* ...