A two digit month could not be found Data missing in Laravel
Asked Answered
T

2

5

I'm having a table where I'm storing the data in date format through datepicker. While storing the date I'm doing something like this:

$data['schedule'] = Carbon::parse($request->schedule)->toDateTimeString();

then in model I've defined like following:

protected $dates = [
    'schedule', 'created_at', 'updated_at', 'deleted_at'
];

Now while retrieving the date I'm using diffForHumans() something like this:

$event->schedule = $event->schedule->diffForHumans();

But it is throwing an error like this:

A two digit month could not be found. Data missing

I checked mySQL database for the format , I'm having dates like this:

2017-07-02 14:38:23

Edit:

Previously I was having an issue while storing the date in tables, please refer to How to format date receiving through Vuejs Datepicker in laravel for the detailed description. It throws error of:

Invalid datetime format: 1292 Incorrect datetime value: '2017-05-04T18:30:00.000Z' for column 'schedule' at row 1

So I chose to use Carbon::parse while storing it in database.

How to resolve this, help me out.

Thanks.

Tosch answered 3/7, 2017 at 8:14 Comment(6)
the problem is you are already changing the format before in $data['schedule'] comment it out and checkClef
@Clef If I don't use Carbon::parse... then it is not accepting values in tableTosch
What do you get if you do dd($request->schedule); before $data['schedule']?Leonialeonid
ok so you need to format the datetime while its being accessed in the carbon, like the format you have in the databaseClef
@Clef Yes you can say I'm looking for accessor.Tosch
i guess you can pass the format in the toDateTimeString or diffForHumans to format it according to your need. get through the documentation i guess it will help youClef
M
4

Try with an accessor method in your model.

public function getScheduleAttribute($value)
{
    return Carbon::parse($value)->diffForHumans();
}

and access the attribute as

$event->schedule;
Maharajah answered 3/7, 2017 at 8:50 Comment(0)
C
2

Try to set datetime format ('Y-m-d H:i:s')

and run

Chopfallen answered 3/7, 2017 at 8:31 Comment(2)
You mean as mutator?Tosch
Yes @NitishKumar Its helping you or not?Chopfallen

© 2022 - 2024 — McMap. All rights reserved.