PHP How to return datetime(6) from Mysql?

前端 未结 6 1824
隐瞒了意图╮
隐瞒了意图╮ 2020-12-17 10:37

When I query a dateTime(6) PHP is truncating my 6 fractional seconds.

Here is an example of my php code:

$sql = \'SELECT date FROM tbl\';
        $         


        
相关标签:
6条回答
  • 2020-12-17 11:17

    You can format it to include it. The %f includes the six-digit microseconds.

     SELECT DATE_FORMAT(date, '%Y-%m-%d %H:%i:%s.%f') FROM tbl
    
    0 讨论(0)
  • 2020-12-17 11:20

    This bug was found in the Mysql (PECL) package of PHP and had been in an unresolved state for years. It was finally fixed in PHP v7.3.0.

    For related bug details: https://bugs.php.net/bug.php?id=76386

    0 讨论(0)
  • 2020-12-17 11:24

    The problem is with the PHP Adapter you are using to communicate with Mysql. As far as I can see you are using PDO adapter to fetch the query result. However, this adapter does not support fractional seconds (YYYY-MM-DD HH:MM:SS.F).

    The PDO adapter supports YYYY-MM-DD HH:MM:SS and automatically formats the value of datetime columns to this format.

    This is a bug in PDO adapter itself. Please find the link below for reference: http://grokbase.com/t/php/php-bugs/11524dvh68/php-bug-bug-54648-new-pdo-forces-format-of-datetime-fields

    0 讨论(0)
  • 2020-12-17 11:25

    DateTime does not support split seconds (microseconds or milliseconds etc.)

    http://www.php.net/manual/en/class.datetime.php

    https://bugs.php.net/bug.php?id=51950

    0 讨论(0)
  • 2020-12-17 11:28

    in your case you might save the value in MySQL as VARCHAR and case / convert it to datetime when reading it.

    I had the same problem and nothing helped me except converting

    0 讨论(0)
  • 2020-12-17 11:36

    It's crazy this still isn't officially supported by mysqli in PHP 7.1, but anyway, here's a work around.

    select concat(`DateTime6`)`DateTime6` from ...
    

    Basically casting it as a string worked for me, as ugly as that is.

    0 讨论(0)
提交回复
热议问题