adam78
adam78

Reputation: 10078

Carbon create date trailing data error

I'm trying to create a carbon date as follows to store in a timestamp column:

 'from_dt' => Carbon::createFromFormat('Y-m-d', Carbon::now()->year . '-04-01'),
 'to_dt' => Carbon::createFromFormat('Y-m-d', Carbon::now()->addYear() . '-03-31'),

But I'm getting an [InvalidArgumentException] Trailing data exception.

In my model I have set the protect dates property as follows:

// ensure dates are accessed and set as a date
protected $dates = ['from_dt', 'to_dt'];

What the correct way to set a date using carbon and how can I automatically work out the to_dt one year from the from_dt - currently I'm having to hard code the day and month of the to_dt.

Upvotes: 1

Views: 11970

Answers (2)

Ahmed Awan
Ahmed Awan

Reputation: 377

I have also same issue . i was using wrong format. Now it is fix by following code

$dob                          = Carbon::createFromFormat('d-m-Y', $input['date_of_birth']);

$input['date_of_birth']       = $dob->format('Y-m-d');

Upvotes: 0

adam78
adam78

Reputation: 10078

Managed to fix it. Solution below.

'from_dt' => Carbon::parse(Carbon::now()->year . '-04-01'),
'to_dt' => Carbon::parse(Carbon::now()->addYear()->year . '-03-31'),

Upvotes: 1

Related Questions