duanchi4184 2018-09-13 14:28
浏览 47
已采纳

ReturnDatesAsStrings => true返回带小数和零的时间戳

I'm using PHP 7.2, MS SQL Server, fpdf, and phpSpreadsheet to create PDF and Excel reports of my data. Initially I had trouble returning the timestamp (date) fields in the reports, but adding "ReturnDatesAsStrings"=>true in my connection.php solved that problem.

The issue now is that the date fields on the reports have a decimal and zeros added and show up as 2018-03-01 16:43:19.0000000. How can I stop the addition of .0000000? I'm not sure if it makes it any more complicated that it also needs to work with fpdf and phpSpreadsheet, but I included that info just in case.

UPDATE: Zhorov's first answer was right. I removed "ReturnDatesAsStrings"=>true from connection.php and the phpSpreadsheet files work great! On the fpdf files, I had to add in '$date_as_string = date_format($data['created_date'], 'Y-m-d H:i:s');' and then the PDFs work too.

FINAL SOLUTION: NULL values were causing a problem, but Zhorov's fix worked. Had to add $date_as_string = is_null($row['DateField']) ? '' : date_format($row['DateField'] , 'Y-m-d H:i:s');

  • 写回答

2条回答 默认 最新

  • douke1905 2018-09-13 14:43
    关注

    Use ReturnDatesAsStrings => false connection option and date_format() PHP function.

    When ReturnDatesAsStrings is false, datetime, Date, Time, DateTime2, and DateTimeOffset types will be returned as PHP Datetime types. So format these fields as PHP dates with custom format.

    <?php
    
    # Connection
    $server = 'server\instance,port';
    $cinfo = array(
        "ReturnDatesAsStrings"=>false,
        "Database"=>'database',
        "UID"=>"uid",
        "PWD"=>"pwd"
    );
    $conn = sqlsrv_connect($server, $cinfo);
    if( $conn === false )
    {
        echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
        exit;
    }
    
    # Statement
    ...
    $sql = 'SELECT [DateField] FROM [Table]';
    $stmt = sqlsrv_query($conn, $sql);  
    if ($stmt === false) {
        echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
        exit;
    }
    while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
        $date_as_string = is_null($row['DateField']) ? '' : date_format($row['DateField'] , 'Y-m-d H:i:s');
    
        # I'm not familiar with PhpSpreadsheet, but based on documantation, this should work.
        $spreadsheet->getActiveSheet()->setCellValue('A'.$row , $data->OBJECTID);
        $spreadsheet->getActiveSheet()->setCellValue('B'.$row , $data->Inspector);
        $spreadsheet->getActiveSheet()->setCellValue('C'.$row , $date_as_string);
    
        echo $date_as_string;
        echo '</br>';
    }
    ...
    
    # End
    sqlsrv_free_stmt($stmt);
    sqlsrv_close($conn);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元