A service for processing string-based time references into carbon instances
davealex/laravel-time-period-reference is a Laravel package for a service for processing string-based time references into carbon instances.
It currently has 1 GitHub stars and 6 downloads on Packagist (latest version v1.0.9).
Install it with composer require davealex/laravel-time-period-reference.
Discover more Laravel packages by davealex
or browse all Laravel packages to compare alternatives.
Last updated
A clean, flexible utility package for Laravel that converts natural-language time period references (e.g., "3 hours ago", "1 week") into precise Carbon instances.
This is ideal for handling user inputs, API requests, or configuration values that define a time window relative to now(). It supports custom units and integrates seamlessly with PHP 8.1+ Enums.
You can install the package via Composer:
composer require davealex/laravel-time-period-reference
By default, the package supports common references (seconds, minutes, days, weeks, etc.). To customize or extend these references, publish the configuration file:
php artisan vendor:publish --provider="Davealex\LaravelTimePeriodReference\LaravelTimePeriodReferenceServiceProvider" --tag="config"
This creates config/laravel-time-period-reference.php.
The array keys correspond to the standard Carbon method name (e.g., subDays, subWeeks), and the values are the acceptable string references.
<?php
return [
'units' => [
// Keys map to Carbon's sub[Unit]() methods (e.g., subSeconds(X))
'seconds' => ['second', 'seconds', 'sec'],
'minutes' => ['minutes', 'minute', 'min'],
'hours' => ['hours', 'hour', 'hrs'],
'days' => ['days', 'day'],
// ... and so on
],
];
The core functionality is exposed via the Facade, the Service Container, or a helper function (if you choose to define one).
Use the toCarbonInstance() method with a string value like "2 days ago", "5 years", or "10 min". The method returns a fully configured Carbon instance relative to the current time.
use Davealex\LaravelTimePeriodReference\Facades\LaravelTimePeriodReference;
// Note: You must import the Facades namespace
// The reference string is parsed for the number and unit.
$carbonInstance = LaravelTimePeriodReference::toCarbonInstance('2 days ago');
// $carbonInstance is now Carbon::now()->subDays(2)
// Output: e.g., 2024-10-26 (if today is 2024-10-28)
dd($carbonInstance->toDateString());
For clean, type-safe code, the package supports PHP 8.1+ BackedEnums whose values are the reference strings.
// Define your Enum
enum TimeReferenceEnum: string
{
case TWO_WEEKS_AGO = '2 weeks ago';
case FIVE_YEARS = '5 years';
}
// Pass the Enum instance directly to the service
$carbonInstance = LaravelTimePeriodReference::toCarbonInstance(TimeReferenceEnum::FIVE_YEARS);
// $carbonInstance is now Carbon::now()->subYears(5)
You can also type-hint the service class in your controllers or other services:
use Davealex\LaravelTimePeriodReference\LaravelTimePeriodReference;
class ReportController extends Controller
{
public function __construct(
private LaravelTimePeriodReference $timeReferenceService
) {}
public function show(string $period)
{
// $period might be '3 months' from a route parameter
$startTime = $this->timeReferenceService->toCarbonInstance($period);
// ... fetch data using $startTime
}
}
The package throws a single exception, Davealex\LaravelTimePeriodReference\Exceptions\InvalidTimeReferenceCarbonInstance, which covers all parsing and creation errors:
To run the package tests, use Composer:
composer test
or
./vendor/bin/phpunit
Please see CHANGELOG.md for more information on what has changed recently.
Please feel free to contribute by submitting issues or pull requests.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
This package is open-source and available under the MIT license.