Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mdavis1982/duration
A PHP library for converting between different types of durations.
https://github.com/mdavis1982/duration
Last synced: 3 months ago
JSON representation
A PHP library for converting between different types of durations.
- Host: GitHub
- URL: https://github.com/mdavis1982/duration
- Owner: mdavis1982
- License: mit
- Created: 2019-03-18T16:29:55.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-03-19T15:59:38.000Z (almost 6 years ago)
- Last Synced: 2024-05-02T04:21:48.023Z (8 months ago)
- Language: PHP
- Size: 9.77 KB
- Stars: 11
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.org/mdavis1982/duration.svg?branch=master)](https://travis-ci.org/mdavis1982/duration)
# Duration
Duration is a PHP library for converting between different types of durations.For example, you might want to cache something for 2 days, but your cache configuration is in seconds. Duration makes this super easy to read in your code and does away with the magic numbers.
## Installation
Install the package using Composer:
```bash
composer require matthewdavis/duration
```## Usage
```php
use MatthewDavis\Duration\Duration;$seconds = Duration::days(3)->inSeconds(); // 259,200
```The library has the following static constructors:
```php
// Seconds
Duration::second();
Duration::seconds(int $seconds);// Minutes
Duration::minute();
Duration::minutes(int $minutes);// Hours
Duration::hour();
Duration::hours(int $hours);// Days
Duration::day();
Duration::days(int $days);// Weeks
Duration::week();
Duration::weeks(int $weeks);
```You can convert between any of the units by using the getter methods:
```php
$duration = Duration::week();$duration->inSeconds(); // 604,800
$duration->inMinutes(); // 10,080
$duration->inHours(); // 168
$duration->inDays(); // 7
$duration->inWeeks(); // 1
```You can also chain the call (as you would likely do when setting a config value):
```php
// In some configuration file...
'cache_ttl' => Duration::days(2)->inSeconds(),
```## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.Please make sure to update tests as appropriate.
## License
[MIT](./LICENSE.md)