如果我删除了它的var_dump

时间:2019-07-03 11:04:15

标签: php sql-server datetime var-dump

我正在尝试创建一个简单的表来显示2019年6月以来的数据,但是无论何时删除var_dump行,我都无法使MSSQL datetime行正确地回显。

因此,使用var_dump可以获取正确的日期,但我的所有列都不会显示为1970年以来的日期,但是当我首先对交易日期进行一次vumpump时,我在所有行上都得到了正确的结果??

这与我需要将行格式更改为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>

下面是礼拜堂

  

object(DateTime)#1(3){[“ date”] =>字符串(26)“ 2016-07-04 00:00:00.000000” [“ timezone_type”] => int(3)[“ timezone “] => string(13)”欧洲/伦敦“}

这是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)[产品折扣组] => [最终产品销售成本] => 165.19999694824 [最终产品其他价值] => 165.2 [最终产品销售价值] => 369.60000610352 [最终产品分析数量] => 1 [发票帐户] => [魔术编号] => 800713 [销售订单行号] => [订单号] => 319651 [PAC级别1] => d [PAC级别2] => dLB [PAC 3级] => dLB01 [PAC 4级] => dLB0106 [产品代码] => [推销员1] => 9500 [声明帐户] => [供应商帐户] => [联系方式] =>传真[订单来源] =>传真[用户] => [客户订单号] =>)

1 个答案:

答案 0 :(得分:1)

当您将PHP驱动程序用于SQL Server而非PDO版本时,可以通过指定ReturnDatesAsStrings选项将retrieve date and time types(日期时间,日期,时间,日期时间2和日期时间偏移)作为字符串或PHP日期时间对象使用在连接字符串中:

<?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,因此您只需要格式化datetime对象。

<?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>
   ...
}
...

?>