cocur/human-date
Transforms DateTime values into human‑readable strings like Today, Tomorrow, Yesterday, Next Tuesday, or formatted dates. Supports translated strings via a translation interface (including Symfony Translation). Lightweight, no external dependencies, PSR‑4, PHP 5.4+ and HHVM.
Installation:
composer require cocur/human-date
No additional autoload configuration is required for Laravel 5.5+ (PSR-4 compliant).
First Use Case:
Convert a Carbon instance to a human-readable string:
use Cocur\HumanDate\HumanDate;
use Carbon\Carbon;
$date = Carbon::now();
$humanDate = new HumanDate($date);
echo $humanDate->get(); // Outputs: "a few seconds ago" or "just now"
Where to Look First:
HumanDate class methods: get(), getRelative(), and getAbsolute().Basic Date Formatting:
$humanDate = new HumanDate($carbonDate);
echo $humanDate->get(); // "2 hours ago"
Relative vs. Absolute:
getRelative() for time-ago strings (e.g., "yesterday").getAbsolute() for formatted dates (e.g., "Jan 1, 2023").echo $humanDate->getRelative(); // "last week"
echo $humanDate->getAbsolute(); // "Jan 1, 2023"
Customizing Output: Override defaults via constructor:
$humanDate = new HumanDate($carbonDate, [
'precision' => 2, // Show "2 hours ago" instead of "2 hours and 30 minutes ago"
'language' => 'fr', // French translations (if supported)
]);
Integration with Laravel:
HumanDate to the container for easy dependency injection:
$this->app->bind(HumanDate::class, function ($app) {
return new HumanDate($app->make(Carbon::class));
});
AppServiceProvider's boot():
if (!function_exists('human_date')) {
function human_date($date = null, array $options = [])
{
$carbonDate = $date ?? now();
return (new \Cocur\HumanDate\HumanDate($carbonDate, $options))->get();
}
}
Usage:
echo human_date($post->created_at); // "3 days ago"
Dynamic Date Handling: Useful in views or controllers for timestamps:
// In a Blade view:
<p>Posted {{ human_date($post->created_at) }}</p>
Initial Release Limitations:
BraincraftedHumanDateBundle and may lack some features or stability.Language Support:
Precision Quirks:
precision option may not behave as expected for edge cases (e.g., dates exactly 1 minute apart).getRelative() or getAbsolute() to inspect formatting.Time Zone Sensitivity:
Carbon instance uses the correct time zone:
$date->setTimezone('America/New_York');
Caching Static Outputs:
HumanDate instance to avoid redundant calculations:
$posts->each(function ($post) {
$post->humanReadableDate = cache()->remember(
"human_date_{$post->id}",
now()->addHours(1),
fn() => human_date($post->created_at)
);
});
Inspect Raw Methods:
Call getRelative() and getAbsolute() separately to isolate issues:
var_dump($humanDate->getRelative(), $humanDate->getAbsolute());
Check Carbon Input:
Verify the Carbon instance’s values before passing to HumanDate:
dd($carbonDate->toDateTimeString());
Override Defaults:
If translations or formatting are off, subclass HumanDate:
class CustomHumanDate extends \Cocur\HumanDate\HumanDate
{
public function __construct(\DateTimeInterface $date)
{
parent::__construct($date, [
'language' => 'custom',
'translations' => [
'just now' => 'justo ahora',
// ...
],
]);
}
}
Add Custom Rules:
Extend the logic by subclassing and overriding methods like _getRelativeTimeString() or _getAbsoluteTimeString().
Plugin System: Create a trait or decorator to add pre/post-processing:
trait HumanDateEnhancer
{
public function getEnhanced()
{
$relative = $this->getRelative();
return str_replace('ago', 'since', $relative); // Example modification
}
}
Database Storage: Store human-readable dates in the DB for non-technical users, but ensure they’re regenerated on read:
// Migration:
$table->text('human_readable_date')->nullable();
// Model:
public function getHumanReadableDateAttribute()
{
return human_date($this->created_at);
}
How can I help you explore Laravel packages today?