Invalid datetime format: 1292 Incorrect datetime value [duplicate]
Matthew Harrington
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
My PHP code uses 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
Existing data
31 Answer
You need to format date like "Y-m-d H:i:s" in order to work with MySQL datetime field.
i.e. :
$lastupdated = date('Y-m-d H:i:s');From documentation :
6The 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'.