php insert with datetime format

1

This is the following I have 2 variables:

Variable date format (yyyy / mm / dd):

$inserir_data = nl2br( addslashes($_POST['inserir_data']));

And a VARCHAR type shift that contains 2 hours is in the format (hh: mm-hh: mm):

$inserir_turno = nl2br( addslashes($_POST['inserir_turno']));

I used this to separate the shift into 2 variables and then added the normal date with the shift to form a date time and insert into the database in a variable of type date time:

$turno_separado = explode('-', $inserir_turno);

$hora_inicio = "$alterar_data $turno_separado[0]";
$hora_final  = "$alterar_data $turno_separado[1]";

The problem is that from what I see everything is correct but it does not insert into the database in the database is always (00-00-00 00:00:00)

Help me that I have no idea what's going on here is how I'm inserting it? due to the fact that in the database it is datetime and it does not recognize the variable?

Thank you for your patience !! I hope you can help me!

    
asked by anonymous 01.01.2018 / 21:24

1 answer

2

Sample table

First let's take as a base the following table created in mysql:

+-------------------------+
|datetime_inicio datetime |
+-------------------------+
|datetime_fim    datetime | 
+-------------------------+

Fixing the current code

And now make your code work (before suggesting any improvement!). Your code has only a small error at run time. Apparently the variable $alterar_hora was undefined (I imagine you wanted to use $inserir_data ). Making this change, the code looks like this:

<?php

//formato ano/mes/dia ou YYYY/mm/dd
$_POST['inserir_data'] = '2018/1/1';
$inserir_data = nl2br( addslashes($_POST['inserir_data']));

//formato ano/mes/dia ou YYYY/mm/dd
$_POST['inserir_turno'] = '10:30-11:40';
$inserir_turno = nl2br( addslashes($_POST['inserir_turno']));

$turno_separado = explode('-', $inserir_turno);

$hora_inicio = "$inserir_data $turno_separado[0]";
$hora_final  = "$inserir_data $turno_separado[1]";

var_dump($hora_inicio);
var_dump($hora_final);

$conexao = mysqli_connect('host', 'usuario', 'senha', 'nome_banco');
mysqli_query($conexao, 'insert into teste (datatime_inicio, datetime_fim) values (\'' 
. $hora_inicio . '\',\'' . $hora_final .'\')');

//exibir erros na execução da instrução anterior (remover em produção).
echo mysqli_error($conexao);

What's different about your code are escapes "\ '" to allow a single quote to be inside another single quotation mark, since strings must be enclosed in quotation marks. After a few executions of the above code, the table in the database will look similar to:

+----------------------+--------------------+
|datetime_inicio       |datetime_fim        |
+----------------------+--------------------+
|2018-01-01 10:30:00   |2018-01-01 11:40:00 |
|2018-01-01 10:30:00   |2018-01-01 11:40:00 |
|2018-01-01 10:30:00   |2018-01-01 11:40:00 |
+----------------------+--------------------+

Improvements

Of course you could use something similar to this <input type="datetime-local" name="bdaytime"> tag to get the full datetime, and only validate it on the server. Although this does not work on all browsers (but to use plugins that allow this).

    
01.01.2018 / 23:32