I have a Microsoft database and I make a query that returns a series of values. Everything is fine except when I want to get the date, something does not work and I do not know what it is.
Here is the result of the query:
Array ( [0] => DateTime Object ( [date] => 2017-04-10 00:00:00.000000 [timezone_type] => 3 [timezone] => Europe/Berlin ) [1] => DateTime Object ( [date] => 2017-04-10 00:00:00.000000 [timezone_type] => 3 [timezone] => Europe/Berlin ) ) Array ( [0] => DateTime Object ( [date] => 2017-04-07 00:00:00.000000 [timezone_type] => 3 [timezone] => Europe/Berlin ) [1] => DateTime Object ( [date] => 2017-04-07 00:00:00.000000 [timezone_type] => 3 [timezone] => Europe/Berlin ) ) Array ( [0] => DateTime Object ( [date] => 2017-04-04 00:00:00.000000 [timezone_type] => 3 [timezone] => Europe/Berlin ) [1] => DateTime Object ( [date] => 2017-04-04 00:00:00.000000 [timezone_type] => 3 [timezone] => Europe/Berlin ) ) Array ( [0] => DateTime Object ( [date] => 2017-04-10 00:00:00.000000 [timezone_type] => 3 [timezone] => Europe/Berlin ) [1] => DateTime Object ( [date] => 2017-04-10 00:00:00.000000 [timezone_type] => 3 [timezone] => Europe/Berlin ) ) Array ( [0] => DateTime Object ( [date] => 2017-04-10 00:00:00.000000 [timezone_type] => 3 [timezone] => Europe/Berlin ) [1] => DateTime Object ( [date] => 2017-04-10 00:00:00.000000 [timezone_type] => 3 [timezone] => Europe/Berlin ) ) Array ( [0] => DateTime Object ( [date] => 2017-04-07 00:00:00.000000 [timezone_type] => 3 [timezone] => Europe/Berlin ) [1] => DateTime Object ( [date] => 2017-04-07 00:00:00.000000 [timezone_type] => 3 [timezone] => Europe/Berlin ) ) Array ( [0] => DateTime Object ( [date] => 2017-04-10 00:00:00.000000 [timezone_type] => 3 [timezone] => Europe/Berlin ) [1] => DateTime Object ( [date] => 2017-04-10 00:00:00.000000 [timezone_type] => 3 [timezone] => Europe/Berlin ) )
How can i get the value of "date"? I try with this but i don't get any result:
echo '</table>';
echo '</div>';
echo '<div class="tbl-content">';
echo '<table cellpadding="0" cellspacing="0" border="0">';
echo '<tbody>';
while ($row = sqlsrv_fetch_array($result)){
echo "<tr>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td>$row[2]</td>";
echo "<td>$row[4]</td>";
if($row[7]== '1') {
echo "<td>Si</td>";
} else if($row[7]== '0') {
echo "<td>'No'</td>";
}
echo "<td>$row[5]</td>"; //NO RESULT THERE
echo "</tr>";
}
echo '</tbody>';
echo '</table>';
echo '</div>';
PHP objects have their own properties and methods. You cannot simply echo (print) the object and expect to get a string. Checking documentation or var_dump
ing an object can give you the method or property you should use to access the string value. In this case it is a DateTime object so you should be able to use DateTime::format('Y-m-d')
to access the date and format it.
In your specific case you should be able to use $row[0]->format('Y/m/d H:i:s')
for each of your date objects to get them as string. Obviously changing the index where applicable.
As a general rule, you cannot print objects unless they have a __toString()
method that makes sense. The DateTime
class doesn't:
echo new DateTime();
# Recoverable fatal error: Object of class DateTime could not be converted to string
Just format to you liking:
echo (new DateTime())->format('c');
# 2017-04-11T11:11:28+02:00
echo (new DateTime())->format('r');
# Tue, 11 Apr 2017 11:11:49 +0200
echo (new DateTime())->format('d/m/Y');
# 11/04/2017
You're also overlooking that you need to generate HTML, not plain text:
echo '<td>' . htmlspecialchars($row[0]->format('d/m/Y')) . '</td>';