A readable and fluent way to generate PHP cache time.
Built and written by Ajimoti Ibukun
Instead of this:
$cacheDuration = 25 * 60 * 60; // twenty five hours
Redis::expire($userData, $cacheDuration);
You can do this
$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:
$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);
- PHP 8.0 or higher
You can install the package via composer:
composer require ajimoti/cache-duration --with-all-dependencies
After installing the package via composer, import the Duration
trait inside your class, then you are set.
<?php
require 'vendor/autoload.php';
use Ajimoti\CacheDuration\Duration;
var_dump(Duration::fourtyMinutes()); // returns 2400;
var_dump(Duration::tenHours()); // returns 36000;
var_dump(Duration::fiftyFourDays()); // returns 4665600;
Method | Expectations |
---|---|
seconds($value) | Expects time in seconds |
minutes($value) | Expects time in minutes |
hours($value) | Expects time in hours |
days($value) | Expects time in days |
at($value) | Expects string , carbon instance, or DateTime instance |
In addition to the methods provided above, the package uses PHP
__callStatic()
method to allow you make dynamic calls on the Duration
trait.
For example, you want to get the number of seconds in 37 days, you can achieve this by calling a camel-case
text of the number (thirtySeven
in this case), plus the unit (Days
in this case). That will leave us with something like this:
// The formula = camelCaseOfTheNumberInWords + Unit
Duration::thirtySevenDays(); // returns the number of seconds in 37 days
Note: The number in words MUST be in
camel-case
. Any other case will throw anInvalidArgumentException
. Additionally, it must be followed by atitle-case
of the unit. The available units areSeconds
,Minutes
,Hours
, andDays
.
Get time in seconds. It basically returns the same value passed into it.
use Ajimoti\CacheDuration\Duration;
$cacheDuration = Duration::seconds(30); // returns 30
// or dynamically
$cacheDuration = Duration::thirtySeconds(); // returns 30
Converts time in minutes into seconds.
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)
Converts time in hours into seconds.
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)
Converts time in days into seconds.
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)
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
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.
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.