KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Formatting MSSQL Time datatype #1236

Open Andrew Guattery opened this topic on on Dec 30, 2019 - 5 comments

Andrew Guattery commented on Dec 30, 2019

Greetings All! Just starting with KoolReport Pro, and have run into an issue with MSSQL "time" datatype. It seems that no matter what I try no formatting is applied! My column code looks like so:

"CLCK_TIM"=>array(
                "label"=>"Clock Time",
                "type"=>"time",
                "format"=>"H:i:s.u",
                "displayFormat"=>"H:i",
            ),

The data returned in my Table view is always in the same format as it is stored in the table IE 12:20:15.00000 I have tried changing the "type" to datetime and fussing with the "format" and "displayformat" values with no luck. Can anyone help with this? Andy

Andrew Guattery commented on Dec 30, 2019

An update: I am obviously missing something, as I cannot format the date column either:

"TIMCRD_DAT"=>array(
                "label"=>"Start Month",
                "type"=>"datetime",
                "format"=>"Y-n",
                "displayFormat"=>"F, Y"
            )

This works in the first test report I created but is not working in my new report.

Andrew Guattery commented on Dec 30, 2019

Update number two: in doing

var_dump($this->dataStore('mydata');

has revealed this:

 ["TIMCRD_DAT"]=> string(23) "2019-12-13 00:00:00.000" ["SEQ_NO"]=> string(1)  ["REF"]=> string(0) "" ["CLCK_TIM"]=> string(16) "08:12:33.0000000"

both the TIMCRD_DAT and CLCK_TIM field are coming through as strings rather than datetime. TIMCRD_DAT is MSSQL type datetime and CLCK_TIM is type time. A little more searching turned up this from Microsoft:

This feature, added in version 5.6.0, is only valid when using the PDO_SQLSRV driver for the Microsoft Drivers for PHP for SQL Server.

To retrieve date and time types as DateTime objects
When using PDO_SQLSRV, date and time types (smalldatetime, datetime, date, time, datetime2, and datetimeoffset) are by default returned as strings. Neither the PDO::ATTR_STRINGIFY_FETCHES nor the PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE attribute has any effect. In order to retrieve date and time types as PHP DateTime objects, set the connection or statement attribute PDO::SQLSRV_ATTR_FETCHES_DATETIME_TYPE to true (it is false by default).

Now being on version 5.6.1 if I modify PDODatasource.php with the following (line 119):

$this->connection->setAttribute(PDO::SQLSRV_ATTR_FETCHES_DATETIME_TYPE, true);

That will produce this error:

[30-Dec-2019 22:17:42 UTC] PHP Warning:  htmlspecialchars() expects parameter 1 to be string, object given in C:\inetpub\wwwmymac\koolreport_pro_4.3.2\koolreport_pro-4.3.2\koolreport\core\src\widgets\koolphp\Table.tpl.php on line 137
[30-Dec-2019 22:17:42 UTC] PHP Warning:  DateTime::createFromFormat() expects parameter 2 to be string, object given in C:\inetpub\wwwmymac\koolreport_pro_4.3.2\koolreport_pro-4.3.2\koolreport\core\src\core\Utility.php on line 169
[30-Dec-2019 22:17:42 UTC] PHP Recoverable fatal error:  Object of class DateTime could not be converted to string in C:\inetpub\wwwmymac\koolreport_pro_4.3.2\koolreport_pro-4.3.2\koolreport\core\src\widgets\koolphp\Table.tpl.php on line 138

So that isn't the answer. Am I missing something?

David Winterburn commented on Dec 31, 2019

Hi Andrew,

Regarding CLCK_TIM time column, I think the problem is because of the number of nanosecond digit "0000000", which has 7 of them, while the nanosecond format "u" of PHP only allows maximum 6 digits. In case your data doesn't need to be precise to a 10^-7 of a nanosecond you could omit 1 zero from your returned data by either:

  1. Change your SQL Server setting for your column.

  2. Use the following format:

    "format"=>"H:i:s.u0", // assuming that the last digit is always 0, like "08:12:33.1234560" would be good, but "08:12:33.1234561" wouldn't

For TIMCRD_DAT date column, a good format would be:

    "format" => "Y-m-d H:i:s.u"

Let us know how this works for you. Thanks!

Andrew Guattery commented on Dec 31, 2019

Ah precision..... We certainly do not need 10^-7 precision for employee time clock calculations :-) Changing the decimal to 6 (still way overkill, but it matches PHP default so ok) not only fixed the "time" formatting but also the date! I had tried "Y-m-d H:i:s.u" with no luck, but after changing the precision on the "time" field date formatting magically worked! Just getting started with Koolreports, looking forward to the 7 day tutorial (soon as I can find the time :-) )

Happy New Year, and thanks for the helpful nudge. Andy

David Winterburn commented on Jan 7, 2020

Warmly welcome, Andy!

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
None yet

None