如何将 MSSQL 日期时间字段 $row 回显到 PHP

How to echo a MSSQL datetime field $row into PHP

我正在尝试创建一个简单的 table 来显示 2019 年 6 月以来的数据,但是我无法正确获取 MSSQL 日期时间行。最初它破坏了我的代码并且是空白的,然后我尝试使用 date() strtotime php 函数,它现在只显示所有列的 31 01 1970,这是不正确的。

这是输入 2016-07-04 00:00:00.000

的示例

下面是我的代码。

    <?php
$serverName = "SQL,1433";
$connectionInfo = array( "Database"=>"", "UID"=>"", "PWD"=>"");
$conn = sqlsrv_connect( $serverName, $connectionInfo );


$sql = "SELECT TOP 1 * FROM [all sales data]";
$stmt = sqlsrv_query( $conn, $sql);
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
?>

<tr>
                    <td><?php echo $row['Order Point Account'];?></td>
                    <td><?php echo $row['Analysis Category'];?></td>
                    <td><?php echo $row['Branch Code'];?></td>
                    <td><?php echo date("d m Y", strtotime($row->date));?></td>
                    <td><?php echo $row['Product Discount Group'];?></td>
                    <td><?php echo $row['End Product Cost of Sale'];?></td>
                    <td><?php echo $row['End Product Other Value'];?></td>
                    <td><?php echo $row['End Product Sales Value'];?></td>
                    <td><?php echo $row['End Product Analysis Quantity'];?></td>
                    <td><?php echo $row['Invoice Account'];?></td>
                    <td><?php echo $row['Magic Number'];?></td>
                    <td><?php echo $row['Order Number'];?></td>
                    <td><?php echo $row['PAC Level 1'];?></td>
                    <td><?php echo $row['PAC Level 2'];?></td>
                    <td><?php echo $row['PAC Level 3'];?></td>
                    <td><?php echo $row['PAC Level 4'];?></td>
                    <td><?php echo $row['Product Code'];?></td>
                    <td><?php echo $row['Salesman 1'];?></td>
                    <td><?php echo $row['Statement Account'];?></td>
                    <td><?php echo $row['Supplier Account'];?></td>
                    <td><?php echo $row['Contact']?></td>
                    <td><?php echo $row['Order Source'];?></td>
                    <td><?php echo $row['User'];?></td>
                    <td><?php echo $row['Customer Order No'];?></td>
                </tr>

            <?php
            }
            ?>


            </tbody>
            </table>
    </body>
</html>

下面是 vardump

object(DateTime)#1 (3) { ["date"]=> string(26) "2016-07-04 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/London" }

这是 print_r($row);

的结果

Array ( [Order Point Account] => [Analysis Category] => AD [Branch Code] => 100 [Transaction Date] => DateTime Object ( [date] => 2016-07-04 00:00:00.000000 [timezone_type] => 3 [timezone] => Europe/London ) [Product Discount Group] => [End Product Cost of Sale] => 165.19999694824 [End Product Other Value] => 165.2 [End Product Sales Value] => 369.60000610352 [End Product Analysis Quantity] => 1 [Invoice Account] => [Magic Number] => 800713 [Sales Order Line Number] => [Order Number] => 319651 [PAC Level 1] => d [PAC Level 2] => dLB [PAC Level 3] => dLB01 [PAC Level 4] => dLB0106 [Product Code] => [Salesman 1] => 9500 [Statement Account] => [Supplier Account] => [Contact] => fax [Order Source] => FAX [User] => [Customer Order No] => )

知道我做错了什么才能正确显示这些数据吗?

非常感谢!

您的代码中有 4 个问题:

问题 1:

你这里有错字$row,[Transaction Date]

问题 2:

您的列名称是 date 而不是 Transaction Date

问题 3:

您的回复以对象形式而非数组格式。

第 4 期:

您正在使用 t 日期格式,return Number of days in the given month

<td><?php echo date("t m Y", strtotime($row,['Transaction Date']));?></td>

应该是:

<td><?php echo date("d m Y", strtotime($row->date));?></td>

PHP Manual