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

Ap3twe's avatar

Getting the value of date (Y:M:D)?

I wanna get only Y:M:D. In laravel the created_at returns Y,M,D, hour, minutes and second. I tried to use public function getFromDateAttribute($value) { return \Carbon\Carbon::parse($value)->format('d-m-Y'); } in the user model. It still returns 2019-05-15 20:38:42

0 likes
7 replies
tisuchi's avatar

What if you try this way?

Carbon\Carbon::createFromFormat('d-m-Y', '2019-05-15 20:38:42');
1 like
Ap3twe's avatar

@tisuchi Did not work. A question though is the getFromDateAttribute a global method from laravel? I copied that function on stackoverflow.

tisuchi's avatar

@AP3TWE - BTW, I have tested your code. If I pass the code like this way, it works fine.

$user = User::first();

return \Carbon\Carbon::parse($user->created_at)->format('d-m-Y');

What is the purpose of wrapping this code by attribute method?

Ap3twe's avatar

@TISUCHI - I wasn't sure. Did you put it in the User model or controller?

Cronix's avatar
Cronix
Best Answer
Level 67

created_at (and updated_at) should already be a carbon instance if working with an Eloquent Model. All you should have to do is format it.

Like in the view

{{ $user->created_at->format('d-m-Y') }}

Please or to participate in this conversation.