I am trying to pull the data out from mssql server table to print html table in webpage using php, the data i am pulling down is “DateTime ” column and when i run the script i got error message saying ” Object of class DateTime could not be converted to string “
and the below is my script. can anyone point me correct way? Thank you so much in advance.
$sql = "SELECT DateCreated FROM Item WHERE Inactive = '0' ";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
$html_table = '<table border="1" cellspacing="0" cellpadding="2">';
$html_table .= '<tr><td width="100" align="center">' .$row['DateCreated'].'</td></tr>';
$html_table .= '</table>';
echo $html_table;
}
sqlsrv_free_stmt( $stmt);
One way to do it would be like this to have the SQL Engine itself return a string value in your first line of code. There might be more performat ways to convert on the PHP end but I’m not as good with that side of the work.
There are a lot of other date format standards you can modify with the third parameter found in this guide from microsoft. The 101 that I used gives the date back in mm/dd/yyyy format, but literally every format under the sun including times, months names, centuries, milliseconds etc. can be returned by using one of the other codes.