在非对象上调用成员函数format()


Fatal error: Call to a member function format() on a non-object in

也许将我的帖子标记为副本,我搜索了但找不到我的问题的答案,我已经尝试了所有我看到的其他帖子的答案,但仍然给我同样的错误:致命错误:在非对象上调用成员函数format()…这是代码:

<?php
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="Movimientos Cancelados del Mes.xls"');
header("Pragma: no-cache");
header("Expires: 0");
$server = "192.168.1.240";
$info = array("Database"=>"Ariel","UID"=>"sa","PWD"=>"" );
$conn = sqlsrv_connect($server, $info);
$param = array('ReturnDatesAsStrings'=> true);
$opt = array("Scrollable" => SQLSRV_CURSOR_KEYSET);
$per = $_GET["periodo"];
$eje = $_GET["ejercicio"];
$mov = 'Movimiento';
$est = 'Estatus';
$cli = 'Cliente';
$rfc = 'RFC';
$tot = 'Total';
$fec = 'Fecha Timbrado';
$uuid = 'UUID';
$cert = 'Certificado SAT';
$sql = "select v.MovID as '$mov',v.Estatus as '$est',v.Cliente as '$cli',cte.rfc as '$rfc',(v.Importe+v.Impuestos)as '$tot', c.UUID as '$uuid',c.noCertificadoSAT as '$cert', c.FechaTimbrado as '$fec'
from Venta V join CFD c on v.MovID = c.MovID join cte on v.cliente = cte.cliente 
where V.Estatus = 'Cancelado' and c.Periodo = '$per' and c.Ejercicio = '$eje' and  c.Empresa = 'MGJ' 
order by FechaEmision";
var_dump($fec);
$query = sqlsrv_query($conn, $sql);
if( $query === false ) {
    if( ($errors = sqlsrv_errors() ) != null) {
        foreach( $errors as $error ) {
            echo "SQLSTATE: ".$error[ 'SQLSTATE']."<br />";
            echo "code: ".$error[ 'code']."<br />";
            echo "message: ".$error[ 'message']."<br />";
        }
    }
}
$campos = sqlsrv_num_fields($query);
$i = 0;
echo "<table border=''><tr>";
echo "<th>$mov</th>";
echo "<th>$est</th>";
echo "<th>$cli</th>";
echo "<th>$rfc</th>";
echo "<th>$tot</th>";
echo "<th>$uuid</th>";
echo "<th>$cert</th>";
while ($row = sqlsrv_fetch_array($query)) {
    $mov = $row['Movimiento'];
    $est = $row['Estatus'];
    $cli = $row['Cliente'];
    $rfc = $row['RFC'];
    $tot = $row['Total'];
    $uuid = $row['UUID'];
    $cert = $row['Certificado SAT'];
    $fec = $row['Fecha Timbrado'];
    echo "<tr>";
    echo "<td>".$mov."</td>";
    echo "<td>".$est."</td>";
    echo "<td>".$cli."</td>";
    echo "<td>".$rfc."</td>";
    echo "<td>".$tot."</td>";
    echo "<td>".$uuid."</td>";
    echo "<td>".$cert."</td>";
    echo "<td>".$fec->format("d/m/Y")."</td>";
}
echo "</table>";
   sqlsrv_close( $conn);
?>

和看和看在互联网上,但我发现它给了我错误,有趣的是,只有一些结果显示与错误,有一些,如果打印在屏幕上,但在一些行之后,给出错误它可能是在第一,它可能是在查询的末尾,忽略前几行代码,是为了导出到Excel…错误标记在第75行,即:

echo "<td>".$fec->format("d/m/Y")."</td>";

作为社区wiki发布,因为它是在对op的多个建议的评论中解决的。

If (! empty($variable)) echo($variable); else echo ('NULL');

是问题的最终解决方案