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

irslan's avatar

How to change the format of all dates in Laravel8 project

Hi, I am very new to Laravel. Never seen tons of directories before. I am trying to update an existing inventory system made in Laravel 8. I want to change the date format to d-m-Y wherever the date() is defined. The current date format is (Y-M-d). It's very difficult to locate the forms, table, and JS files. Please help me how to start with. Thanks in advance

0 likes
4 replies
AungHtetPaing__'s avatar

@irslan define accesor for attribute.

<?php
 
namespace App\Models;
 
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
 
class User extends Model
{
    /**
     * Get the user's birth date.
     *
     * @return \Illuminate\Database\Eloquent\Casts\Attribute
     */
    protected function birthDate(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => $value->format('d-m-Y'), //you need to cast date as carbon instance
        );
    }
}
jlrdw's avatar

You can use carbon to display (format) the date for display, but store in database YYYY-MM-DD.

SilenceBringer's avatar

@irslan if dates defined as dates in eloqent model and they use explicit format in blade, like

$model->created_at->format('Y-m-d')

you need to change it. You can search by code and replace all occurences

or explain how they display it

1 like
irslan's avatar

Thanks everyone. I define this function:

public function getDateAttribute($value) { return date('d-m-Y',strtotime($value)); }

I changed this:

to

{{ __('custom.date') }}: {{$invoice->date}}

It worked. Thanks

Please or to participate in this conversation.