Dates not saved as expected

Published 2 years ago by Khudadad

I'm using a date picker in front end and when submit the form the date changed and another date is stored in the database (i.e 2016-30-05 is changed to 2018-06-05). any help.

Code:

    $date = $request->input('buy-date');
           $purchaseDate = Carbon::createFromFormat('d/m/Y',$date );
           $newPurchase->buy_date = $purchaseDate->toDateString();
Best Answer (As Selected By Khudadad)
Swaz

Okay, you might have your day and month mixed up then. Try:

Carbon::createFromFormat('m/d/Y', $date);
Swaz
Swaz
2 years ago (57,455 XP)

What date picker are you using? Some of them submit the date in a different format than what is presented on screen. What does dd($request->input('buy-date')); give you?

Khudadad

I'm using https://github.com/Eonasdan/bootstrap-datetimepicker and the result of dd($request->input('buy-date')); is OK. The date is changed after bellow operation

$purchaseDate = Carbon::createFromFormat('d/m/Y',$date );
Swaz
Swaz
2 years ago (57,455 XP)

Okay, you might have your day and month mixed up then. Try:

Carbon::createFromFormat('m/d/Y', $date);
Khudadad

Oh, God. yeah worked.

Thanks @Swaz

Please sign in or create an account to participate in this conversation.