问题
I have small laravel project working on date conversion. I have date string that getting from request on format dd/mm/yyyy. Code and result show as below.
$request->stockupdate ;
// dd/mm/yyyy (02/05/2019)
Then I try to convert to yyyy-mm-dd using carbon.
$_stockupdate= Carbon::parse($request->stockupdate)->format('Y-m-d');
I got parse result as below.
2019/02/05 // Seem it is 2 Feb 2019 not 5 May 2019.
That's wrong, It should be 2019/05/02 instead. Any advise or guidance would be greatly appreciated, Thanks.
回答1:
You can try this:
Carbon::createFromFormat('d/m/Y', $request->stockupdate)->format('Y-m-d')
回答2:
You can try this :
$date = str_replace('/', '-', $request->stockupdate);
$newDate = date("Y-m-d", strtotime($date));
OR
Carbon::createFromFormat('d/m/Y', $request->stockupdate)->format('Y-m-d')
来源:https://stackoverflow.com/questions/55950681/how-to-convert-date-format-from-dd-mm-yyyy-to-yyyy-mm-dd-using-carbon-on-laravel