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

vincej's avatar
Level 15

How to format a Carbon Date inside Blade ?

My Blade file runs a foreach on the variable $open_quotes. This variable is an array generated from a query on the DB. It contains the DB value "created_at". It all works well, except that the "created_at" date comes out on the HTML as Year Month Day Minutes Seconds. I want the date presented as Day Month Year Minutes.

I know you can format the date inside Laravel, but can I do that inside a Blade file ?

 @foreach($open_quotes as $quotes)
            <tr>
                <td> <a href="quote_edit/{{ $quotes->reference}}">{{$quotes->reference}}</a></td>
                <td> {{$quotes->lastname1}}</td>
                <td> {{$quotes->address}}</td>
                <td>{{$quotes->created_at}}</td>   // THIS NEEDS TO BE REFORMATTED 
                <td>{{$quotes->status}}</td>
            </tr>
        @endforeach

0 likes
14 replies
Jaytee's avatar

Literally chain on to it.

If you want it in human like twitter (2 hours ago etc)

do

{{ $quotes->created_at->diffForHumans() }}

You can also do ->format() etc For more, check out the Carbon documentation.

15 likes
otepas's avatar

Carbon extends DateTime, so you can use DateTime::format. To get Day Month Year Minutes, try this:

{{ $quotes->created_at->format('d m Y i');
6 likes
bobbybouwmann's avatar

I already told you how to do it in your previous question...

3 likes
vincej's avatar
Level 15

@DPJack @otepas

Thanks guys for looking at this, I tried both your ideas, and both came back with the same error message:

FatalErrorException in 5019648ec0411f66a8f5bb4779ba8d14 line 31: Call to a member function format() on a non-object

FatalErrorException in 5019648ec0411f66a8f5bb4779ba8d14 line 31: Call to a member function diffForHumans() on a non-object

vincej's avatar
Level 15

@bobbybouwmann HI Bobby - thanks for that. Maybe I misunderstood your answer. The way I read your answer required that I format this inside Laravel. I want to format the date inside Blade.

Have I misunderstood ? Thanks in advance !

otepas's avatar

Hi @vincej, it looks like what you have is a string and not a Carbon object.

It going to look a bit weird, but you will have to create the Carbon instance from the string you have and then reformat it.

{{ Carbon\Carbon::createFromFormat('Y m d i s', $quotes->created_at)->->format('d m Y i') }}
vincej's avatar
Level 15

@otepas Thanks for that. Unfortunately that does not work either, I get the error:

ErrorException in Carbon.php line 414: Unexpected data found. Unexpected data found. Unexpected data found. Trailing data (View: /var/www/auburntree/resources/views/quotations/open_quotes.blade.php)

ABDELRHMAN's avatar
Level 5

try that

{{ Carbon\Carbon::parse($quotes->created_at)->format('d-m-Y i') }}
27 likes
vincej's avatar
Level 15

Yeah !!!! That did it !!

Many thanks - you rock !

1 like
Jaytee's avatar

You shouldn't need to create a whole instance. The way i listed should work so there is something wrong with your dates.

thomaskim's avatar

@DPJack He's probably not using Eloquent/models. I'm guessing vincej is using the query builder.

vincej's avatar
Level 15

@DPJack Wonder what that could be .. my model:

$protected timestamps = true

MySql has the record type set up as "TIMESTAMP" and in the DB it is stored as: 2016-03-02 16:26:05

should the Type be something else ?

vincej's avatar
Level 15

@thomaskim @DPJack I am using eloquent. Because I have a lot of JQuery calculated fields in my form, plus a have a 35 row form made up of arrays, I am using it like this:

Much abbreviated:

     $newQuotation = new Quotation();
            $newQuotation->reference = $ref;
            $newQuotation->customer_id = $C->id;
            $newQuotation->product_id = $id[$i];
            $newQuotation->quantity = $qty[$i];
            $newQuotation->job_firstname = $job_firstname;
            $newQuotation->job_lastname = $job_lastname;
            $newQuotation->job_email = $job_email;
          
            $newQuotation->save();

Jaytee's avatar

@vincej

Try removing

protected $timestamps;

Also you could do:

Quotation::create([
    'reference' => $request->reference
// etc. clean your code up a bit.
]);

:)

Please or to participate in this conversation.