Back to directory
mdavis1982 avatar

duration

A PHP library for converting between different types of durations.. Read more below about its uses, features, and usage.

Clone repository

git clone https://github.com/mdavis1982/duration.git

11

Stars

1

Forks

2

Watchers

MIT

License

Build Status

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:

composer require matthewdavis/duration

Usage

use MatthewDavis\Duration\Duration;

$seconds = Duration::days(3)->inSeconds(); // 259,200

The library has the following static constructors:

// 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:

$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):

// 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

Releases

Mar 19, 2019

Code Quality Changes

Download .zip

Added

Added an interface

Changed

Enabled strict types Moved constants from the class to the interface Changed references to constants to use the interface constants Changed occurrences of new self...

Mar 18, 2019

Minor Fixes

Download .zip

Changed

Changed the visibility of the constructor and $seconds property.

Removed

Removed an unnecessary return $this in the constructor of the Duration class.