Using Carbon to return a human readable datetime difference

Using Carbon to return a human readable datetime difference

  1. Blog
  2. 15 November 2026
  3. 2 min read

Stefan Bogdanescu

Founder & Senior Architect

Using Carbon to return a human readable datetime difference

When building a comments section or any feed that displays timestamps, simply rendering a raw created_at column isn't enough. Users expect relative time strings like "10 mins ago" or "2 weeks ago," not a static ISO 8601 string. While standard PHP date formatting can achieve this, Laravel's ecosystem offers a more robust solution through the Carbon library.

In older versions of Laravel, specifically Laravel 4 as referenced in recent discussions, Eloquent automatically casts created_at, updated_at, and deleted_at columns into instances of Carbon. This means that when you access $post->created_at, you aren't getting a DateTime object or a string; you are getting a fully featured Carbon instance.

However, a common stumbling block arises when developers attempt to create custom date columns. If you return a simple array containing just the time column without proper casting, Laravel treats it as a raw string or a native DateTime object, bypassing Carbon's helper methods entirely. The code snippet return array('time'); suggests an attempt to pass data back to the view, but if the underlying model attribute isn't explicitly cast to a Carbon instance, you lose access to the .diffForHumans() method that generates those relative time strings.

To achieve the Facebook-style "10 mins ago" output, you must ensure the value is treated as a Carbon instance within your controller or service layer. The most direct approach is utilizing the diffForHumans() method provided by the Carbon library. This method calculates the difference between the current moment and the target date, formatting it intelligently based on thresholds (e.g., switching from "hours" to "days" once the 24-hour mark is crossed).

Here is how you implement this in a Laravel controller when returning JSON or data to a view:

// Assuming $post is your Eloquent model instance
$humanReadableTime = $post->created_at->diffForHumans();

return response()->json([
    'time' => $humanReadableTime,
]);

If you are dealing with a custom created_at column that hasn't been automatically cast, you need to explicitly tell Eloquent to treat it as a Carbon instance. In the model definition, add the attribute casting:

protected $casts = [
    'time' => 'datetime',
];

This configuration ensures that accessing $model->time returns a Carbon\CarbonInterface object regardless of Laravel's version nuances. Once cast correctly, you can chain diffForHumans() directly onto the attribute.

It is important to note that diffForHumans() relies on the now() method by default. If your application operates in different time zones, this behavior might vary unless configured globally via App\Providers\AppServiceProvider. You should ensure your Laravel environment is set to handle time zone offsets correctly so that "10 mins ago" reflects the server's or user's local time as intended.

The Carbon package extends the native PHP DateTime class, providing a consistent interface for these operations. Whether you are on Laravel 4 or newer versions where Eloquent casting behaviors have evolved, the principle remains: ensure your date column is an instance of Carbon before attempting to manipulate its relative time representation.

Our network