laravel 碳创建日期尾随数据错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32542772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:20:13  来源:igfitidea点击:

Carbon create date trailing data error

phplaravellaravel-5php-carbon

提问by adam78

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 dataexception.

但我得到了一个[InvalidArgumentException] Trailing data例外。

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.

什么是使用 carbon 设置日期的正确方法,以及如何从 from_dt 自动计算出一年的 to_dt - 目前我必须对 to_dt 的日期和月份进行硬编码。

采纳答案by adam78

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'),

回答by Ahmed Awan

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');