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
- Host: GitHub
- URL: https://github.com/jeromegamez/duration-php
- Owner: jeromegamez
- License: mit
- Created: 2019-01-24T16:13:13.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2024-07-13T19:41:19.000Z (almost 2 years ago)
- Last Synced: 2025-01-07T10:08:51.254Z (over 1 year ago)
- Topics: duration, php, time
- Language: PHP
- Size: 54.7 KB
- Stars: 39
- Watchers: 5
- Forks: 9
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
README
# Duration for PHP
Working with durations made easy.
[](https://packagist.org/packages/gamez/duration)

[](https://codecov.io/gh/jeromegamez/duration-php)
[](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))
* ...