2017-04-11 109 views
2

我有一個Microsoft數據庫,我做了一個查詢,返回一系列值。一切都很好,除非我想得到日期,有些東西不起作用,我不知道它是什麼。MSSQL上獲取datetime

下面是該查詢的結果:

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)) 

如何才能得到「日期」的價值?我嘗試用這個,但我沒有得到任何結果:

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

你爲了得到那個結果而進行了什麼調試?它的奇怪,你有一個JSON字符串作爲結果時,'$行'應該是一個數組 –

+0

對不起@Jason喬斯林壞複製粘貼,現在我把正確的文字 – Pablo

+0

哦,我看到它返回作爲日期時間對象。你可以試試$ row [0] - > format('Y/m/d H:i:s') –

回答

2

PHP對象有自己的屬性和方法。你不能簡單地回顯(打印)對象並期望得到一個字符串。檢查文檔或對象可以爲您提供您應該用來訪問字符串值的方法或屬性。在這種情況下,它是一個DateTime對象,因此您應該可以使用DateTime::format('Y-m-d')來訪問日期並對其進行格式化。

http://php.net/manual/en/book.datetime.php

在特定情況下,你應該能夠使用$row[0]->format('Y/m/d H:i:s')爲每個約會的對象,使他們作爲字符串。顯然在適用的情況下更改索引。

+0

謝謝!工作完美無瑕,我不知道我們不能直接打印日期時間。 – Pablo

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>';