https://github.com/ajimoti/cache-duration
A readable and fluent way to generate PHP cache time.
https://github.com/ajimoti/cache-duration
cache cache-duration laravel-cache php php-cache symfony-cache
Last synced: 3 months ago
JSON representation
A readable and fluent way to generate PHP cache time.
- Host: GitHub
- URL: https://github.com/ajimoti/cache-duration
- Owner: ajimoti
- License: mit
- Created: 2022-01-19T19:41:45.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2023-10-10T00:00:03.000Z (over 2 years ago)
- Last Synced: 2025-09-27T17:33:36.711Z (3 months ago)
- Topics: cache, cache-duration, laravel-cache, php, php-cache, symfony-cache
- Language: PHP
- Homepage:
- Size: 58.6 KB
- Stars: 25
- Watchers: 1
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
# PHP Cache Duration

## Introduction
A readable and fluent way to generate PHP cache time.
Built and written by [Ajimoti Ibukun](https://www.linkedin.com/in/ibukun-ajimoti-3420a786/)
## Quick Samples
Instead of this:
```php
$cacheDuration = 25 * 60 * 60; // twenty five hours
Redis::expire($userData, $cacheDuration);
```
You can do this
```php
$cacheDuration = Duration::twentyFiveHours(); // returns 25 hours in seconds (25 * 60 * 60)
Redis::expire($userData, $cacheDuration);
// or
$cacheDuration = Duration::hours(25); // returns 25 hours in seconds (25 * 60 * 60)
Redis::expire($userData, $cacheDuration);
```
You can also do this:
```php
$cacheDuration = Duration::at('first day of January 2023'); // returns the time difference between the present time and the first of january 2023 in seconds
Redis::expire($userData, $cacheDuration);
```
## Requirements
- PHP 8.0 or higher
## Installation
You can install the package via composer:
```bash
composer require ajimoti/cache-duration --with-all-dependencies
```
## Documentation
After installing the package via composer, import the `Duration` trait inside your class, then you are set.
```php
**Note:** The number in words **MUST** be in `camel-case`. Any other case will throw an `InvalidArgumentException`. Additionally, it must be followed by a `title-case` of the unit. The available units are `Seconds`, `Minutes`, `Hours`, and `Days`.
## Usage
### `seconds($value)`
Get time in seconds. It basically returns the same value passed into it.
```php
use Ajimoti\CacheDuration\Duration;
$cacheDuration = Duration::seconds(30); // returns 30
// or dynamically
$cacheDuration = Duration::thirtySeconds(); // returns 30
```
### `minutes($value)`
Converts time in minutes into seconds.
```php
use Ajimoti\CacheDuration\Duration;
$cacheDuration = Duration::minutes(55); // returns 55 minutes in seconds (55 * 60)
// or dynamically
$cacheDuration = Duration::fiftyFiveMinutes(); // returns 55 minutes in seconds (55 * 60)
```
### `hours($value)`
Converts time in hours into seconds.
```php
use Ajimoti\CacheDuration\Duration;
$cacheDuration = Duration::hours(7); // returns 7 hours in seconds (7 * 60 * 60)
// or dynamically
$cacheDuration = Duration::sevenHours(); // returns 7 hours in seconds (7 * 60 * 60)
```
### `days($value)`
Converts time in days into seconds.
```php
use Ajimoti\CacheDuration\Duration;
$cacheDuration = Duration::days(22); // returns 22 days in seconds (22 * 24 * 60 * 60)
// or dynamically
$cacheDuration = Duration::twentyTwoDays(); // returns 22 days in seconds (22 * 24 * 60 * 60)
```
### `at($value)`
This method allows you to convert a `Carbon\Carbon` instance, `DateTime` instance or `string` of date into seconds.
It returns the difference in seconds between the argument passed and the current `timestamp`.
> The date passed into this method **MUST** be a date in the future. When a string is passed, the text **MUST** be compatible with `Carbon::parse()` method, else an exception will be thrown
#### Examples
```php
use Date;
use Carbon\Carbon;
use Ajimoti\CacheDuration\Duration;
// Carbon instance
$cacheDuration = Duration::at(Carbon::now()->addMonths(3)); // returns time in seconds between the present timestamp and three months time
// Datetime instance
$cacheDuration = Duration::at(new DateTime('2039-09-30')); // returns time in seconds between the present timestamp and the date passed (2039-09-30).
// String
$cacheDuration = Duration::at('first day of January 2023'); // returns time in seconds between the present timestamp and the first of January 2023.
```
## Testing
```bash
composer test
```
## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.
## Security Vulnerabilities
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
## Credits
- [ajimoti](https://github.com/ajimoti)
- [All Contributors](../../contributors)
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.