Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jgautier's avatar

Automatically formatting all created_at and updated_at dates in blade views

Hi, Is there's a way to automatically apply a Carbon formatting method to all created_at and updated_at models dates when we use these attributes in blade views ? For ex., in a blade view, when I write :

$post->created_at

I'd like something like "Fri 12 May 2023 19:05:50" to be print, instead of "2023-05-12 19:05:50".

Thanks a lot. JG

0 likes
10 replies
LaryAI's avatar
Level 58

Yes, you can create a custom Blade directive to format all created_at and updated_at dates in your Blade views. Here's an example:

  1. Create a new Blade directive in your AppServiceProvider:
use Illuminate\Support\Facades\Blade;
use Carbon\Carbon;

public function boot()
{
    Blade::directive('datetime', function ($expression) {
        return "<?php echo Carbon\Carbon::parse($expression)->format('D d M Y H:i:s'); ?>";
    });
}
  1. Use the datetime directive in your Blade views to format created_at and updated_at dates:
{{ $post->created_at->datetime() }}

This will output the date in the format you specified: "Fri 12 May 2023 19:05:50".

jgautier's avatar

@dacfabre Thanks for your (human) answer ! But I thought that casting will affect also dates storing in database. No ? I just want to format these properties when displaying them in blade views.

dacfabre's avatar

@jgautier you make your own casts class and setup your get and set method, you can then apply it to all your model. more explaination by laravel daily using money https://www.youtube.com/watch?v=VViQBqC8Dbc

protected $casts = [
        'created_at' => 'datetime:D d M Y H:i:s',
        'updated_at' => 'datetime:D d M Y H:i:s'
   	];

will also not affect your dates in storing

Snapey's avatar
Snapey
Best Answer
Level 122

Use an accessor with the date under a different name so that your existing use of the datetime stamps are unchanged

eg, in the model

public function getFormattedCreatedAtAttribute()
{
	return $created_at->format('D d M Y H:i:s');
}

then in your blade views

{{ $model->formattedCreatedAt }}

Of course your view will receive the dates as a Carbon instance so if you only do this occasionally, if might be quicker to format in the view

{{ $model->created_at->format('D d M Y H:i:s') }}

Another approach is a blade component that does the same;

<x-dateFormatter :date="$model->created_at" />
jgautier's avatar

@Snapey Thanks. In each model, so. No way to do that for all models created_at and updated_at dates at once ?

jgautier's avatar

@Snapey I'm not very familiar with traits... But I'm gonna look better at this approach. Thanks again.

newbie360's avatar

@jgautier

app\Traits\FormattedDate.php

<?php

namespace App\Traits;

trait FormattedDate
{
    public function getFormattedCreatedAtAttribute()
    {
        return $created_at->format('D d M Y H:i:s');
    }

    public function getFormattedUpdatedAtAttribute()
    {
        return $updated_at->format('D d M Y H:i:s');
    }
}
<?php

namespace App\Models;

use App\Traits\FormattedDate;

class ModelA extends Model
{
    use FormattedDate;
<?php

namespace App\Models;

use App\Traits\FormattedDate;

class ModelB extends Model
{
    use FormattedDate;
2 likes
newbie360's avatar

@jgautier Or for any attributes

app\Traits\FormatDate.php

<?php

namespace App\Traits;

trait FormatDate
{
    public function formatDate(string $attribute)
    {
        return $this->{$attribute}->format('D d M Y H:i:s');
    }
}

open Tinker

> Something::find(1)->formatDate('created_at')

> Something::find(1)->formatDate('updated_at')
2 likes

Please or to participate in this conversation.