davealex/laravel-time-period-reference
Composer 安装命令:
composer require davealex/laravel-time-period-reference
包简介
A service for processing string-based time references into carbon instances
README 文档
README
Laravel Time Period Reference
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.
1. Installation
You can install the package via Composer:
composer require davealex/laravel-time-period-reference
2. Configuration
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.
Example Configuration (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
],
];
3. Usage
The core functionality is exposed via the Facade, the Service Container, or a helper function (if you choose to define one).
Converting a String Reference
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());
Converting a Backed Enum
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)
Manual Service Injection
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
}
}
4. Exceptions
The package throws a single exception, Davealex\LaravelTimePeriodReference\Exceptions\InvalidTimeReferenceCarbonInstance, which covers all parsing and creation errors:
- Invalid Format: (e.g., "days 2", "two days ago") - Numeric value expected before the unit.
- Unknown Unit: (e.g., "2 decades ago") - The unit is not defined in the configuration.
- Carbon Error: An internal error occurred during Carbon instance creation.
5. Testing
To run the package tests, use Composer:
composer test
or
./vendor/bin/phpunit
6. Contribution and License
Changelog
Please see CHANGELOG.md for more information on what has changed recently.
Contributing
Please feel free to contribute by submitting issues or pull requests.
Security
If you discover any security related issues, please email daveabiola@gmail.com instead of using the issue tracker.
Credits
- David Olaleye
- All Contributors
License
This package is open-source and available under the MIT license.
davealex/laravel-time-period-reference 适用场景与选型建议
davealex/laravel-time-period-reference 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 10 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「davealex」 「laravel-time-period-reference」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 davealex/laravel-time-period-reference 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 davealex/laravel-time-period-reference 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 davealex/laravel-time-period-reference 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Alfabank REST API integration
Laravel package for Accurate Online API integration.
Shared RCX Laravel DataTables UI and configuration helpers.
Boot a Laravel project on any machine with one command: app:serve installs missing tools (PHP, Node, Composer, Herd, Docker), creates .env, sets up the database, runs migrations, builds assets, starts a queue worker and serves via Herd, Sail or artisan serve; app:down cleanly stops everything it sta
Branded, diagnostic error pages (500, 403, 404, 419, 503) for Filament — native Filament UI, dark mode and translations out of the box.
Turn any PDF into a Pingen-ready A4 letter (generated address cover page + A4 normalisation + safe margins) and send it through the Pingen print & mail API. Laravel-first.
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-19