Laravel and the treatment of dates

-1

Gentlemen, I'm getting a date in this DD / MM / YYYY format, and since I'm using MYSQL, I have to save it in YYYY-MM-DD.

The issue is that I'm using eloquent and I'm doing something like this:

public function create(Request $request)
$tabla=new Tabla();
$tabla->fecha=$request->fecha
$tabla->save();

here

$request->fecha

I'm getting '20 / 02/2018 'and I need to save it' 2018-20-02 'how can I do it?

    
asked by jose angarita 20.02.2018 в 19:32
source

1 answer

2

Use Carbon and its creation and formatting tools to generate the date:

public function create(Request $request)
{
    $tabla = new Tabla();

    $fecha = Carbon::createFromFormat('d-m-Y', $request->fecha)->toDateString();
    $fecha = $tabla->fecha;

    $tabla->save();
}

Explanation: first you generate a date according to the format you have with the createFromFormat() method and then the toDateString() method will convert that result to yyyy-mm-dd

    
answered by 20.02.2018 в 21:17