Storing custom date format in database I'm using a special frontend library for displaying dates and it also send dates in a different format to the backend. I need to know how I can save this in the database. The format looks like this 31 August, 2015
$date = $request->input('date'); // dd returns 31 August, 2015
$date = date('Ymd', strtotime($date)); // returns 20190831
It seems that the year is going wrong. How can I convert this to the correct date?
You have a different format so you first need to parse it to the correct date. Laravel comes with Carbon which makes this very easy
$date = Carbon::createFromFormat('d F, Y', $request->input('date'));
$date->format('Ymd');
With your model you can just pass in the date object instead of the formatted string
$date = Carbon::createFromFormat('d F, Y', $request->input('date'));
User::create([
'date' => $date
]);
Please sign in or create an account to participate in this conversation.