承接 davealex/laravel-time-period-reference 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

Latest Version on Packagist Total Downloads run-tests

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

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 davealex/laravel-time-period-reference 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 6
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 20
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-10-19