php 无效的日期时间格式:1292 不正确的日期时间值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42490101/
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:36:31  来源:igfitidea点击:

Invalid datetime format: 1292 Incorrect datetime value

phpmysqldatetimepdo

提问by user580950

I am getting below error when I am trying to update a table with a field(datetime)

当我尝试使用字段(日期时间)更新表时出现以下错误

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[2007]: Invalid datetime format: 1292 Incorrect datetime value: '02-27-2017 16:37' for column lastupated

致命错误:未捕获的异常 'PDOException' 带有消息 'SQLSTATE[2007]:无效的日期时间格式:1292 不正确的日期时间值:'02-27-2017 16:37' 列 lastupated

My PHP code uses PDO

我的 PHP 代码使用 PDO

$lastupdated = date('m-d-Y H:i:s');
$run = $conn->prepare($sql);
$run->bindParam(':lastupdated', $lastupdated, PDO::PARAM_STR); 

the SQL the lastupdated, datatype is datetime

最后更新的 SQL,数据类型是 datetime

Existing data

现有数据

enter image description here

在此处输入图片说明

回答by JazZ

You need to format date like "Y-m-d H:i:s"in order to work with MySQL datetime field.

您需要格式化日期"Y-m-d H:i:s"才能使用 MySQL 日期时间字段。

i.e. :

IE :

$lastupdated = date('Y-m-d H:i:s');

From documentation:

文档

The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS'format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.

DATETIME 类型用于同时包含日期和时间部分的值。MySQL 以'YYYY-MM-DD HH:MM:SS'格式检索和显示 DATETIME 值。支持的范围是“1000-01-01 00:00:00”到“9999-12-31 23:59:59”。