Ricardo Romero Ricardo Romero - 10 months ago 49
PHP Question

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

Perhaps mark my post as a duplicate, I searched but can not find answer to my problem, I have tried everything I saw the answers of the other post, but still gives me the same error: Fatal error: Call to a member function format () on a non-object in ... This is the code:


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 = "";
$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";


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

And look and look on the internet, but I find it gives me the error, the funny thing is that only some results are showing with the error, there are some that if printed on screen but after some lines, gives the error it may be at first, it may be near the end of the query, ignore the first few lines of code, is for export to Excel ... The error is marked on the line 75 which is:

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

Answer Source

Posting as a community wiki, since it was solved in comments with multiple suggestions to the OP.

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

being the final solution to the problem.