Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 705
joenpc npcsolution Avatar asked May 02 '19 10:05

joenpc npcsolution


People also ask

How do I change the date format from dd mm yyyy to dd mm yyyy?

Your answer First, pick the cells that contain dates, then right-click and select Format Cells. Select Custom in the Number Tab, then type 'dd-mmm-yyyy' in the Type text box, then click okay. It will format the dates you specify.

How do you convert a string to a Carbon date?

$date = Carbon\Carbon::parse($rawDate); well thats it. You'll now have a Carbon instance & you can format the date as you like using Carbon helper functions.


2 Answers

You can try this:

Carbon::createFromFormat('d/m/Y', $request->stockupdate)->format('Y-m-d')
like image 198
nakov Avatar answered Oct 09 '22 04:10

nakov


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')
like image 2
Jithesh Jose Avatar answered Oct 09 '22 02:10

Jithesh Jose