Problem with dates in LARAVEL

0

I try to change the format of a date created by laravel with timestamps () that saves them in the format 2000-01-01 00:00:00.

I have tried in the view (blade template) within the value of an input type date:

date('y-m-d', strtotime($user->created_at))

I have tried to change the format in the controller and pass it to the already formatted view but I do a dd () and it shows me the date in format 2000-01-01 but when I get to the view I do not know what happens He shows it well.

If I format it in the view or in the driver I get this from the browser console:

  

The specified value "2018-12-06 21:33:09" does not conform to the   required format, "yyyy-MM-dd".

I've also tried this:

$fecha = new Carbon($user->created_at);
$created = $fecha->toDateString();

Same result.

    
asked by Crtisian Navarro 18.12.2018 в 16:21
source

1 answer

2

You have multiple ways. I specify some of them and you choose the one you think best:

1) At the model level, globally, with Carbon:

public function getCreatedAtAttribute($date)
{
    return Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('Y-m-d');
}

public function getUpdatedAtAttribute($date)
{
    return Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('Y-m-d');
}

With those functions in the view you just have to show:

{{ $user->created_at }}

2) At the controller level, with Datetime:

$fecha_ejemplo = Datetime::createFromFormat('d M Y H:i', $user->created_at);

Datetime also has another version with (new):

$aux = new DateTime($user->created_at);
$fecha_ejemplo = $aux->format('Y-m-d H:i:s');

Greetings

    
answered by 18.12.2018 / 16:45
source