23

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

enter image description here

user580950
  • 3,558
  • 12
  • 49
  • 94

1 Answers1

54

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 :

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

JazZ
  • 4,469
  • 2
  • 20
  • 40