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

hsnch41's avatar

Date format issue

I want to save date in 15-12-2018 format.. but error is SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: '25-03-2018' for column 'start_date' at row 1 (SQL: insert into batches (batch_name, start_date, end_date, status, updated_at, created_at) values (Batch 2018, 25-03-2018, 27-04-2018, 1, 2018-04-13 11:25:49, 2018-04-13 11:25:49))

0 likes
4 replies
rin4ik's avatar
rin4ik
Best Answer
Level 50

use Carbon and you can manipulate your date however you want

use Carbon\Carbon;

$date = Carbon::parse($request->start_date);
devonblzx's avatar

You can accept inputs in various formats but you should always convert it to YYYY-MM-DD for use in your application and database. Not only is this what MySQL expects, but it's much easier to work with YYYY-MM-DD for comparisons.

For example, '16-12-2018' > '01-01-2019' which isn't valid.

Cronix's avatar

Mysql expects YYYY-MM-DD HH:ii:ss format. Just store that way and you can format it how you want when displaying.

Let's say you have a model named MyModel and a datetime field called "mydate".

In the MyModel model, you can add

protected $dates = [
    'created_at',
    'updated_at',
    'mydate'
];
// need to include created_at and updated_at in addition to your custom fields

https://laravel.com/docs/5.6/eloquent-mutators#date-mutators

That will convert your mydate field to a carbon instance automatically.

Then in your views, you can

{{ $MyModel->mydate->format('d-m-Y') }}

to format it how you want to display it.

Please or to participate in this conversation.