How to convert date format from dd/mm/yyyy to yyyy-mm-dd using carbon on Laravel

牧云@^-^@ 提交于 2019-12-11 13:43:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!