Home > OS >  timestamp is not saving in database when use strtotime in php
timestamp is not saving in database when use strtotime in php

Time:10-26

i have a problem when i try to insert a time in my database using mysql:

The database is storing the date wrongly as you can see:

enter image description here

The column type is TIMESTAMP

enter image description here

In my code (i am using PHP with codeigniter 4 for this), i have a string date, but the type of the field which is saving the date is TIMESTAMP

$fecha = strtotime($pedido['fecha_creacion']);   //Get the date from string and converting it to timestamp

$dbventor->query("INSERT INTO tfacpeda(FECHA)VALUES ($fecha));   //Insert to database, but wrongly

The variable fecha contains a date string like this: "2022-10-07 15:03:10"

can you help me with this?

thanks!

CodePudding user response:

Problem solved!

the issue comes because a confusion, the timestamp doesn't have to be treated in a different way, because the database is expecting to receive a date string

$fecha = $pedido['fecha_creacion'];

$dbventor->query("INSERT INTO tfacpeda(FECHA)VALUES ('$fecha'));   //Note the quotes at the variable '$fecha'
  • Related