0
I’m trying to concatenate a date and time to get the value (ISO) in Mysql so I can insert them into the database. The code line is this:
Example: datai = 20/04/2018
and horai = 14:27
$data = '$_POST[datai]';
$data2 = date("Y-m-d", strtotime($data)); // converter para formato definido
$hora = '$_POST[horai]'.':00'; // adicionar segundos à hora
$datahora = date('Y-m-d H:i:s', strtotime($data2.''.$hora));
However, what is received in the comic is:
1970-01-01 01:00:00
Can you explain to me if it is possible to concatenate date and time in this way? They are two different inputs.
The query to the database is this:
$inserirdatahora = mysqli_query ($conexao,"UPDATE minha_tabela
SET data_de_inicio = '$datahora' WHERE (id = 10)");
@RBZ not, because the variables would not be evaluated. A string would be literal. See the difference.
– Woss
I just tested. I get it now ! rs
– rbz
I understood the answer very well, but it hasn’t worked yet... Don’t I have to add the ":00" to the hour for example? The query is inserted with right quotes?
– White
@White As for adding zeros, it depends on what you’re going through via post. And in SQL, yes, it should have quotes, but only in SQL, not in the variable itself.
– Woss
I am passing for example 25-10-2017 and 8:00 for hours, as I pass this to ISO already with the code on top?
– White
@White can’t understand where you might still have trouble. Have you ever tested the solution? With these entries the code works perfectly.
– Woss
I changed the question above with example input, can you help? I know we are very close to solving, but I am missing something...
– White
But you changed the format of the date. In the comment is hyphenated, the question is with bar. This makes all the difference for dates. When you use bars, the pattern is
mm/dd/yyyy
and notdd/mm/yyyy
.– Woss
But it’s with "/", I’m sorry. It’s possible to help?
– White
So just go from bar to hyphen and it’ll work.
– Woss
dd-mm-YYYY in this way?
– White