0
I am working with PHP and SQLSRV.
I need to enter a date in a field of the type Datetime
and I can not at all. At least not in the ways I have found in Internet searches.
The last one I tried was this, because they commented that it worked:
if(isset($_POST['cadastrar'])){
$dataInicio = $_POST['data'];
$dataHoje = date("Y-m-d");
$dataInicio['data'] = $_POST['data'];
$data = DateTime::createFromFormat('l, j F, Y', $dataInicio['data']);
$data = $data->format('Y-m-d H:i:s');
$dataHoje['data'] = $_POST['data'];
$dataHj = DateTime::createFromFormat('l, j F, Y', $dataHoje['data']);
$dataHj = $dataHj->format('Y-m-d H:i:s');
$sql = "INSERT INTO [RDO].[dbo].[CAD_FUN] (DATA, DATAINI) VALUES (?,?)";
$params = array($data, $data);
However, when clicking on register, the following warning appears:
Fatal error: Call to a Member Function format() on a non-object
It would be easier to look at the code that handles the INSERT command. But basically it is better to use parameters in your SQL command and pass the date as Datetime even instead of formatting it as string. If you prefer string, the universal MS SQL Server format for data entry is
yyyy-MM-dd hh:mm:ss
.– Caffé
Ah, as for your mistake, it seems
$data
cannot receive aDateTime
. In this case,$_POST['data']
may be empty or contain invalid data. You can debug?– Caffé
Caffé! Follow my code: if(isset($_POST['register']){ $dataInicio = $_POST['data']; $dataHoje = date("Y-m-d"); $dataInicio['data'] = $_POST['data']; $data = Datetime::createFromFormat('l, j F, Y', $dataInicio['data']); $data = $data->format('Y-m-d H:i:s'); $dataHoje['data'] = $_POST['data']; $dataHj = Datetime::createFromFormat('l, j F, Y', $dataHoje['data']); $dataHj = $dataHj->format('Y-m-d H:i:s'); $sql = "INSERT INTO [RDO]. [dbo]. [CAD_FUN] (DATA, DATAINI) VALUES (?,? )"; $params = array($date, $date);
– GustavoSevero
Cool, Gustavo. But you need to edit your question and put the code there instead of in the comments.
– Caffé
Ready Coffee, I put up the code. I hope it helps.
– GustavoSevero