php 无效的日期时间格式:1292 不正确的日期时间值 - Laravel 5.2
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44409105/
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
Invalid datetime format: 1292 Incorrect datetime value - Laravel 5.2
提问by goateee25
I'm getting this error when I insert these value into my database table:
当我将这些值插入我的数据库表时,我收到此错误:
SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '24/06/2017' for column 'registration_from' at row 1 (SQL: insert into `campus_registrations` (`campus_major_class_id`, `registration_from`, `registration_to`, `testing`, `announcement`, `enrollment_from`, `enrollment_to`, `updated_at`, `created_at`) values (3, 24/06/2017, 27/06/2017, 13/07/2017, 01/08/2017, 05/09/2017, 31/01/2018, 2017-06-07 09:39:31, 2017-06-07 09:39:31))
SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '24/06/2017' for column 'registration_from' at row 1 (SQL: insert into `campus_registrations` (`campus_major_class_id`, `registration_from`, `registration_to`, `testing`, `announcement`, `enrollment_from`, `enrollment_to`, `updated_at`, `created_at`) values (3, 24/06/2017, 27/06/2017, 13/07/2017, 01/08/2017, 05/09/2017, 31/01/2018, 2017-06-07 09:39:31, 2017-06-07 09:39:31))
Do I have to intiate the datetime first or what?
我必须先启动日期时间还是什么?
采纳答案by Mayank Pandeyz
The error is here:
错误在这里:
Incorrect datetime value: '24/06/2017' for column 'registration_from' at row 1
the default format for date
column in mysql is Y-m-d
and for datetime
is Y-m-d H:i:s
. So change your date to this format and try again.
date
mysql 中列的默认格式是 ,Y-m-d
而 fordatetime
是Y-m-d H:i:s
。因此,将您的日期更改为此格式并重试。
回答by Ryan Dhungel
I got the same error while trying to register user from the vue js frontend to laravel backend API.
尝试将用户从 vue js 前端注册到 Laravel 后端 API 时,我遇到了同样的错误。
The solution was updating the mysql strict mode to false. If anyone knows the cons of this approach, please leave your comments!
解决方案是将 mysql 严格模式更新为 false。如果有人知道这种方法的缺点,请留下您的评论!
//config/database.php
'mysql' => [
'strict' => false,
]
回答by Alexey Mezenin
Since you're not using standard datetime format, define a mutatorfor each date. For example:
由于您没有使用标准日期时间格式,因此请为每个日期定义一个修改器。例如:
public function setRegistrationFromAttribute($value)
{
$this->attributes['registration_from'] = Carbon::parse($value);
}
回答by Rytis Dereskevicius
Don't forget about Y2K38(Year 2038 problem) -
不要忘记 Y2K38(2038 年问题)-
timestamp
has a limit of 1970-01-01 00:00:01
UTC to 2038-01-19 03:14:07
UTC (source), whilst dateTime
has a range of 1000-01-01 00:00:00
to 9999-12-31 23:59:59
, so consider what you plan on storing in that field before determining which type to use.
timestamp
1970-01-01 00:00:01
UTC 到2038-01-19 03:14:07
UTC(来源)的限制,而dateTime
范围是1000-01-01 00:00:00
to 9999-12-31 23:59:59
,因此在确定使用哪种类型之前,请考虑您计划在该字段中存储的内容。
timestamp
and dateTime
are similar - they store a date (YYYY-MM-DD)
and time (HH:MM:SS)
together in a single field i.e. YYYY-MM-DD HH:MM:SS
. The difference between the two is that timestamp can use (MySQL's) CURRENT_TIMESTAMP
as its value, whenever the database record is updated. This can be handled at the database level and is great for Laravel's created_at
and updated_at
fields.
timestamp
并且dateTime
是相似的 - 它们将日期(YYYY-MM-DD)
和时间(HH:MM:SS)
一起存储在单个字段中,即YYYY-MM-DD HH:MM:SS
. 两者之间的区别在于CURRENT_TIMESTAMP
,无论何时更新数据库记录,timestamp 都可以使用(MySQL 的)作为其值。这可以在数据库级别处理,非常适合 Laravelcreated_at
和updated_at
字段。
date stores just the date component i.e. YYYY-MM-DD
(1000-00-01
to 9999-12-31
).
date 仅存储日期组件,即YYYY-MM-DD
( 1000-00-01
to 9999-12-31
)。
timestamps
doesn't take an argument, it's a shortcut to add the created_at
and updated_at
timestamp fields to your database.
timestamps
不带参数,它是将created_at
和updated_at
时间戳字段添加到数据库的快捷方式。
So setting a date on timestamp bigger than 2038-01-19 03:14:07 UTC
will also throw - SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value
所以在时间戳上设置一个日期大于2038-01-19 03:14:07 UTC
也会抛出 -SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value
回答by Ganesh Khadka
I got the same problem and finally i solved the problem.Yoou can add the few line of code at the end of the add.blade.php
我遇到了同样的问题,最后我解决了问题。您可以在 add.blade.php 的末尾添加几行代码
$('.date-picker').datepicker({
format: 'yy/mm/dd',
autoclose: true,
todayHighlight: true
});
回答by Saumini Navaratnam
回答by Kwaye Kant
This was happening to me under laravel 5.6, until I changed the code to
$table->date('last_date')->default(now());
这在 Laravel 5.6 下发生在我身上,直到我将代码更改为
$table->date('last_date')->default(now());