title: Converting from SQL datetime to PHP DateTime, using
body: <p>The variable <code>time_of_last_update</code> in the database is of type <code>datetime</code>, and all I want to do is really print it out in the table (below) but ideally I would like to know for future reference how to cast it/convert it to a DateTime type in PHP, to then use the methods on it such as <code>-&gt;format</code> etc. Doing:</p>
  
  <pre><code>$time_date = $row['time_of_last_update'];
  $time_date-&gt;format('Y-m-d H:i:s');
  </code></pre>
  
  <p>Seems obvious to me coming from C#, but I get "Call to a member function format() on a non-object", and casting doesn't seem to hep me either. This seems really simple/common, but I cannot find any examples.</p>
  
  <pre><code>$describeQuery = 'SELECT username, firstname, surname, location, time_of_last_update      FROM location';
  $query = sqlsrv_query($link, $describeQuery);
  
  echo '&lt;table&gt;';
  echo '&lt;tr&gt;&lt;th&gt;Username&lt;/th&gt;&lt;th&gt;Firstname&lt;/th&gt;&lt;th&gt;Surname&lt;/th&gt;&lt;th&gt;Location&lt;/th&gt;&lt;th&gt;Time of last Update&lt;/th&gt;&lt;/tr&gt;';
  
  while($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC))
  {
  // WANT TO CONVERT time_of_last_update (SQL datetime) to a PHP DateTime variable??
  
  echo '&lt;tr&gt;';
  echo '&lt;td&gt;' . $row['username'] . '&lt;/td&gt;';
  echo '&lt;td&gt;' . $row['firstname'] . '&lt;/td&gt;';
  echo '&lt;td&gt;' . $row['surname'] . '&lt;/td&gt;';
  echo '&lt;td&gt;' . $row['location'] . '&lt;/td&gt;';
  echo '&lt;td&gt;' . $row['time_of_last_update'] . '&lt;/td&gt;';// RETURNING ERROR "Object of class DateTime could not be converted to string"
  }
  
  echo '&lt;/table&gt;';
  sqlsrv_free_stmt($query);
  sqlsrv_close($link);
  </code></pre>
  
  <p>Thanks</p>