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

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等