Date function with two variables

Asked

Viewed 90 times

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)");

2 answers

1

Yes, it is possible, but as you defined $data as '$_POST[datai]', the variable will receive the string '$_POST[datai]' literal, not the value of $_POST['datai'], because you used simple quotes. As the function strtotime does not recognize this pattern, instead of giving error it will set the default date (typical PHP does not warn the developer that something is wrong). The same goes for $_POST['horai'].

PHP only evaluates the value of a variable within string when double quotation marks are used, as in "Você informou a data {$data}", However, in this case, why the quotes? All data that comes with the HTTP request, by definition, are string, then it is unnecessary to do "$_POST['datai']".

The correct alternative would be:

$data = $_POST['datai'];
$hora = $_POST['horai']; // amém
$datahora = date('Y-m-d H:i:s', strtotime("{$data} {$hora}"));
  • @RBZ not, because the variables would not be evaluated. A string would be literal. See the difference.

  • I just tested. I get it now ! rs

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

  • 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 can’t understand where you might still have trouble. Have you ever tested the solution? With these entries the code works perfectly.

  • I changed the question above with example input, can you help? I know we are very close to solving, but I am missing something...

  • 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 not dd/mm/yyyy.

  • But it’s with "/", I’m sorry. It’s possible to help?

  • So just go from bar to hyphen and it’ll work.

  • dd-mm-YYYY in this way?

Show 6 more comments

0


Response that transforms two variables into one and converts from dd/mm/yyyy for yyyy-mm-dd and adds two '0' at the end of the time (from 8:00 to 08:00:00).

$date1 = strtr($_POST['datai'], '/', '-');
$date2 = date('Y-m-d', strtotime($date1));
$hora = $_POST['horai'].':00';
$datahora = date('Y-m-d H:i:s', strtotime("{$date2} {$hora}"));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.