1

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.

4

2 回答 2

1

设法修复它。下面的解决方案。

'from_dt' => Carbon::parse(Carbon::now()->year . '-04-01'),
'to_dt' => Carbon::parse(Carbon::now()->addYear()->year . '-03-31'),
于 2015-09-12T20:49:38.967 回答
0

我也有同样的问题。我使用了错误的格式。现在通过以下代码修复

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

$input['date_of_birth']       = $dob->format('Y-m-d');
于 2018-05-11T05:58:52.007 回答