Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

podu-file-plus's avatar

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?

0 likes
1 reply
bobbybouwmann's avatar
Level 88

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 or to participate in this conversation.