Updating sqlsrv datetime field

-2

I need to update the date in the field date, of type datetime, in a table, taking the date of today, for example. I'm trying to do this:

$dataHoje = new DateTime();

$sql = "UPDATE [RDO].[dbo].[ANALISE_CRITICA] SET
        TXTOBS='$objetoExtra',
        VLRECEITACONT='$receitaContrato',                                                             VLRECEITABRUTA='$receitaBrutaMarco',
        VLISSQN='$percentualIssqn',
        VLBASE='$baseCalculoIssqn',
        VLIMPOSTTOTAL='$valorTotalImpostos',
        VLCOMISSAO='$comissionamento',
        VLCUSTO='$custoDireto',
        VLADMLOCAL='$admLocal',
        VLRISCO='$risco',
        VLCUSTOFIN='$custoFinanceiro',
        VLADMCENTRAL='$admCentral',
        VLRESULTFIN='$resultadoFinanceiro',
        VLCORRETAGENS='$corretagem',
        dataAlteracao = $dataHoje 
      WHERE ID=$id";

$stmt = @sqlsrv_query( $conn, $sql);

However, when trying to update the system I get the following warning: "Catchable fatal error: Object of class DateTime could not be converted to string" What problem is this?

    
asked by anonymous 22.10.2014 / 19:01

2 answers

3

@GustavoSevero, follow the code example, as link

$dataHoje = new DateTime();
$dataHoje = $dataHoje ->format('d/m/Y'); 
echo $dataHoje ;
    
22.10.2014 / 19:21
1

I took the date formatted as a string using the format () method, passing the format you want, remember of putting quotes in the value of a date in the update.

$dataHoje = new DateTime();
$dataFormatada = $dataHoje->format('Y-m-d H:i:s'); //formato: 2014-10-22 15:06:11

$sql = "UPDATE ....
          dataAlteracao = '$dataFormatada'
        WHERE ID = $id";

If the date comes from an input entered by the user in the format dd / mm / yyyy use the createFromFormat () method to create a date and then call format() to leave the date in the format accepted by the bank yyyy-mm-dd.

<?php
$input = '24/10/2014';

$data = DateTime::createFromFormat('d/m/Y', $input);
$dataFormatada = $data->format('Y-m-d');

echo $dataFormatada; //2014-10-24
    
22.10.2014 / 19:36