如果我删除它的 var_dump,MSSQL 日期时间对象将不会回显正确的日期

MSSQL datetime object won't echo out the correct date if i remove its var_dump

我正在尝试创建一个简单的 table 来显示 2019 年 6 月以来的数据,但是每当我删除 var_dump 行时,我都无法正确回显 MSSQL 日期时间行。

因此,使用 var_dump 我得到了正确的日期,但我的所有列都没有显示为 1970 年的日期,该日期不存在,但是当我首先对交易日期进行 vardump 时,我得到所有行的正确结果??

这与我需要将行的格式更改为 PHP 可以理解的格式有关吗?

这是输入 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) ) {
?>

<?php var_dump( $row['Transaction Date']);?>

<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['Transaction Date']->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] => )

当您使用 PHP 驱动程序用于 SQL 服务器,而不是 PDO 版本时,您可以 retrieve date and time types (datetime, date, time, datetime2, and datetimeoffset) as strings or as PHP datetime 对象通过在连接字符串中指定 ReturnDatesAsStrings 选项:

<?php 
...
$serverName = "SQL,1433";
$connectionInfo = array(
    "ReturnDatesAsStrings"=>false,
    // Set to TRUE if you want to get date values as string.
    //"ReturnDatesAsStrings"=>true,
    "Database"=>'database',
    "UID"=>"username",
    "PWD"=>"password"
);
$conn = sqlsrv_connect( $serverName, $connectionInfo );
...
?>

默认情况下,ReturnDatesAsStringsfalse,所以你只需要格式化日期时间对象。

<?php
...
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
   ...
   // Format date with procedural or object oriented style:
   <td><?php echo date_format($row['Transaction Date'], 'd m Y');?></td>
   <td><?php echo $row['Transaction Date']->format('d m Y');?></td>
   ...
}
...

?>