I am connecting to a MSSQL database with PHP using Microsoft ODBC 11 Driver. I am quering the database successfully and receiving the data i want but having problems displaying it. The array I am fetching from the database has some objects which are dates and I do not know how to display them correctly in a table. Now I am receiving a Catchable fatal error: Object of class DateTime could not be converted to string
And here is the code. The commented code in the second foreach is where I am trying to convert the object into string and display only the date without the other properties of the object.
<?php
header('Content-type: text/html; charset=utf-8');
require_once('sqlcon.php');
$sql = "SELECT * FROM dbo.operations WHERE OperType=4";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
$tableHeaderWritten = false;
echo "<table>";
while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) ) {
if(!$tableHeaderWritten) {
echo "<tr>";
foreach ($row as $columns => $rows) {
//var_dump($row);
echo "<th>$columns</th>";
}
echo "</tr>";
$tableHeaderWritten = true;
}
echo "<tr>";
foreach ($row as $columns => $rows) {
if(is_object($rows)) {
//$results = array();
//$results = $rows->format('Y-m-d H:i:s');
//foreach ($rows as $key => $value) {
//var_dump($value);
//echo "<th>$value</th>";
}
}
echo "<th>$rows</th>";
}
echo "</tr>";
}
echo "</table>";
?>
And this is the dumped array I am fetching:
`array (size=23)
'ID' => int 3756022
'OperType' => int 4
'Acct' => int 1
'GoodID' => int 3
'PartnerID' => int 1
'ObjectID' => int 4
'OperatorID' => int 1
'Qtty' => float 0
'Sign' => int 1
'PriceIn' => float 0
'PriceOut' => float 1.98
'VATIn' => float 0
'VATOut' => float 0
'Discount' => float 0
'CurrencyID' => int 1
'CurrencyRate' => float 1
'Date' =>
object(DateTime)[1]
public 'date' => string '2015-05-25 00:00:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Paris' (length=12)
'Lot' => string ' ' (length=1)
'LotID' => int 1
'Note' => string 'Изтриване на период към 25.05.2015' (length=54)
'SrcDocID' => int 1
'UserID' => int 1
'UserRealTime' =>
object(DateTime)[2]
public 'date' => string '2015-05-26 18:12:53' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Paris' (length=12)`
<table> while ( ...)...loop look something like: Pastebin: 31892252/convert-object-to-string-mssql-php ? Untested.