MSSQL datetime to html printing problem using PHP (sqlsrv extension)


I've got a flexible block of code that allows me to print the results of a query to a simple html table. However, when that table contains a field of type 'datetime', the print() function doesn't work and my loop ends.

In the past, using PHP and MySQL (as opposed to Microsoft SQL Server, which is what I'm using now) printing the date was no problem, so I'm not sure what I'm missing now.

Here's the code:

// $row is the current row.
// $nkeys is the array of (String) keys that correspond to columns in the table.
do {
    print ("<tr>\n");
    foreach($nkeys as $k) {
        echo ("<td>$row[$k]</td>");
    print ("</tr>\n");
} while ($row = sqlsrv_fetch_array($stmt));

Is there a special function other than print() or echo that will properly print the date?

The SQLSRV extension returns DateTime objects. Just format it to your liking:

if( !is_null($row[$k]) ){
    echo $row[$k]->format('r');

In the procedural interface you are using you can also disable the feature by providing a ReturnDatesAsStrings connection option (this option is not available in the PDO interface) but there's normally no reason to do so.