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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:58:59  来源:igfitidea点击:

Invalid datetime format: 1292 Incorrect datetime value - Laravel 5.2

phpmysqllaravel

提问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 datecolumn in mysql is Y-m-dand for datetimeis Y-m-d H:i:s. So change your date to this format and try again.

datemysql 中列的默认格式是 ,Y-m-d而 fordatetimeY-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 年问题)-

timestamphas a limit of 1970-01-01 00:00:01UTC to 2038-01-19 03:14:07UTC (source), whilst dateTimehas a range of 1000-01-01 00:00:00to 9999-12-31 23:59:59, so consider what you plan on storing in that field before determining which type to use.

timestamp1970-01-01 00:00:01UTC 到2038-01-19 03:14:07UTC(来源)的限制,而dateTime范围是1000-01-01 00:00:00to 9999-12-31 23:59:59,因此在确定使用哪种类型之前,请考虑您计划在该字段中存储的内容。

timestampand dateTimeare 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_TIMESTAMPas its value, whenever the database record is updated. This can be handled at the database level and is great for Laravel's created_atand updated_atfields.

timestamp并且dateTime是相似的 - 它们将日期(YYYY-MM-DD)和时间(HH:MM:SS)一起存储在单个字段中,即YYYY-MM-DD HH:MM:SS. 两者之间的区别在于CURRENT_TIMESTAMP,无论何时更新数据库记录,timestamp 都可以使用(MySQL 的)作为其值。这可以在数据库级别处理,非常适合 Laravelcreated_atupdated_at字段。

date stores just the date component i.e. YYYY-MM-DD(1000-00-01to 9999-12-31).

date 仅存储日期组件,即YYYY-MM-DD( 1000-00-01to 9999-12-31)。

timestampsdoesn't take an argument, it's a shortcut to add the created_atand updated_attimestamp fields to your database.

timestamps不带参数,它是将created_atupdated_at时间戳字段添加到数据库的快捷方式。

So setting a date on timestamp bigger than 2038-01-19 03:14:07 UTCwill 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

Reference

参考

MYSQL recognises date value in either YYYY-MM-DDor YY-MM-DDformat. You can use /. This 24/06/2017considered invalid. Try 2017/06/24

MYSQL 以YYYY-MM-DDYY-MM-DD格式识别日期值。您可以使用/. 这24/06/2017被认为是无效的。尝试2017/06/24

回答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());