Error trying to enter a date in a "date" column in the database

0

I have a column type date and I try to insert a date but it throws me the following error:

Invalid parameter value: 7 ERROR: the time zone «gmt-0400» is not recognized (SQL: insert into "lots" ("number", "id_products", "date", "id_turns", "updated_at", "created_at") values (700011, 4, Tue Mar 07 2017 00:00:00 GMT-0400 (Western Standard Time, South America), 11, 2017-03-07 18:37:48, 2017-03-07 18: 37:48) returning "id")

The problem is the use of time, how can I delete it in javascript.

I have a string with the following format: dd/mm/año an ex of this is: 07/03/2017 I pass it to a date with new Date() and I am left as follows: Tue Mar 07 2017 00:00:00 GMT-0400 (Hora estándar oeste, Sudamérica)

The code is the sig:

var fechaInput = $('#crear_fecha').val();
var fecha = new Date(fecha);

NOTE: I do not know which is better to solve, javascript or modify the column of the DB.

    
asked by Pablo Contreras 07.03.2017 в 19:48
source

2 answers

1

You must transform your date to the correct format. For that you can do the following:

echo date('Y-m-d H:i:s',strtotime($fecha));

Where date is the string provided by javascript . Otherwise, instead of generating the date by javascript , you could generate the date from PHP :

$query = "INSERT INTO mytable (fecha) VALUES ('".date('Y-m-d H:i:s')."')";
    
answered by 07.03.2017 в 20:04
0

For this you can transform your "Date" object to a string using the following function: date.toLocaleDateString ();

//var fechaInput = $('#crear_fecha').val();
var fecha = new Date("5/2/2010").toLocaleDateString();
console.log(fecha)

This will return a string of a date in the same format as the one you sent. Ex: 3/8/2017

    
answered by 08.03.2017 в 20:13