- How do I install cocur/human-date in a Laravel project?
- Run `composer require cocur/human-date` in your project root. The package works with Laravel’s Carbon dependency, so no additional setup is needed beyond enabling PHP’s `intl` extension if missing.
- Does cocur/human-date support Laravel’s Eloquent models?
- Yes. Add an accessor to your model (e.g., `public function getCreatedAtHumanAttribute() { return HumanDate::transform($this->created_at); }`) to format timestamps directly in Blade or API responses.
- Can I use this package in Blade templates?
- Absolutely. Inject the package as a helper or create a Blade directive. For example, `@php echo HumanDate::transform($order->created_at) @endphp` renders dates like '3 hours ago' in your views.
- What Laravel versions does cocur/human-date support?
- The package is compatible with Laravel 5.8+ (PHP 7.2+) and Laravel 10+ (PHP 8.x). Test thoroughly with your Laravel version, as the package hasn’t been updated since 2014 but relies on stable Carbon APIs.
- How do I handle localization for non-English dates?
- Use PHP’s `IntlDateFormatter` for basic locale support, but note pluralization rules (e.g., '1 day ago' vs. '2 days ago') may require manual adjustments. For advanced cases, combine with Laravel’s localization or `voku/portable-ascii`.
- Is cocur/human-date safe for production use?
- Yes, but test edge cases like timezone handling and future dates. Cache formatted dates in Blade views or API responses if rendering many timestamps to avoid performance overhead.
- What if the PHP `intl` extension is disabled?
- The package requires `intl`. If unavailable, implement a fallback (e.g., `Carbon::diffForHumans()` or a simple `date()` format) or enable the extension in your PHP configuration.
- Can I customize the output format (e.g., 'in 2 days' vs. '2 days ago')?
- The package uses `HumanDate::transform()` for past dates and `HumanDate::transformFuture()` for future dates. Override the default behavior by extending the class or using a custom translation adapter.
- Are there alternatives to cocur/human-date for Laravel?
- Yes. Consider `Carbon::diffForHumans()` (built into Laravel) for basic needs, or `spatie/array-to-xml` for broader transformations. For advanced localization, evaluate `voku/portable-ascii` or `symfony/intl`.
- How do I test cocur/human-date in a Laravel app?
- Write unit tests for critical use cases (e.g., `HumanDate::transform(now())`, `HumanDate::transform(yesterday())`). Mock the `DateTime` object to simulate edge timestamps like 'now' or future dates.