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 'Created_at' in Blade using Carbon

I have been Googling and trying different recomentations here on Laracasts but without success. Essentially, Carbon formating needs to see an object, but my data is coming off the DB as an array:

array:1 [▼
  0 => {#340 ▼
    +"reference": "Joh170117-61"
    +"quote_name": ""
    +"lead_product": ""
    +"total": "134.34"
    +"job_address": ""
    +"job_city": ""
    +"start_date": "Tue Feb 21"
    +"created_at": "2017-01-17 16:22:44"
    +"company_name": "Private Sale"
    +"firstname1": "Jill"
    +"lastname1": "Johness"
    +"telephone1": "4036853105"
    +"address": "151 Auburn Place"
  }
]

so when it reaches my view the data looks like this:

{{date_format($quotation[0]->created_at}}

So it is presented as 2017-01-17 15:03:22

So what can I do to get the created_at date reformated into d/m/Y in Blade ?

Many thank !

0 likes
4 replies
SaeedPrez's avatar
Level 50

Are you using the query builder instead of Eloquent or how are you getting the data?

Anyhow, you could always parse a string date..

\Carbon\Carbon::parse($quotation[0]->created_at)->format('d/m/Y');
sherwinmdev's avatar

try this

{{ \Carbon\Carbon::createFromFormat('created_at', 'd/m/Y')->toDateTimeString() }}

if you want it to always display a certain way, consider creating accessors https://laravel.com/docs/5.3/eloquent-mutators#accessors-and-mutators

// in your model
public function getMyDateFormat()
{
    return \Carbon\Carbon::createFromFormat($this->attributes['created_at'], 'd/m/Y')->toDateTimeString();
}

or create a helper class with a method that formats the date the way you want

class MyHelper {
    public function myDateFormat($value) {
        return \Carbon\Carbon::createFromFormat($value, 'd/m/Y')->toDateTimeString();
    }
}

// in blade
{{ MyHelper::myDateFormat($record->created_at) }}

something like that

1 like
vincej's avatar
Level 15

Thank you both for coming back ! You are both correct, however I gave the reward point to @SaeedPrez only because he was first.

And Yes - I am using query builder only becauee I have to join 2 tables.

Thanks !

1 like
sherwinmdev's avatar

@vincej no worries. i'm not here for the points. i'm here to share and learn. glad you found the answer.

1 like

Please or to participate in this conversation.