我有一个Microsoft数据库,我创建一个返回一系列值的查询。一切都很好,除非我想要约会,有些东西不起作用,我不知道它是什么。
以下是查询的结果:
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 ) )
我怎样才能获得" date"的价值?我尝试了这个,但我没有得到任何结果:
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>';
答案 0 :(得分:2)
PHP对象有自己的属性和方法。您不能简单地回显(打印)对象并期望得到一个字符串。检查文档或var_dump
对象可以为您提供用于访问字符串值的方法或属性。在这种情况下,它是一个DateTime对象,因此您应该能够使用DateTime::format('Y-m-d')
来访问日期并对其进行格式化。
在您的特定情况下,您应该能够为每个日期对象使用$row[0]->format('Y/m/d H:i:s')
来将它们作为字符串。显然,在适用的情况下更改索引。
答案 1 :(得分:1)
作为一般规则,您不能打印对象,除非它们具有合理的__toString()
method。 DateTime
class没有:
echo new DateTime();
# Recoverable fatal error: Object of class DateTime could not be converted to string
只是format你喜欢:
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
你也忽略了你需要生成HTML而不是纯文本:
echo '<td>' . htmlspecialchars($row[0]->format('d/m/Y')) . '</td>';