KoolReport's Forum

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

Table grouping: sum time #734

Open davide opened this topic on on Mar 9, 2019 - 7 comments

davide commented on Mar 9, 2019

Hi, Koolreport is great! I bought it and I'm very happy.

In a Table I need to group and sum the time of each row.

Inside a grouping array I need to format the sum as time. Something like:

"calculate"=>array(
    "{sumTime}"=>array("sum","worktime"),
    "type"=>"datetime",
    "displayFormat"=>"H:i",
),

Thank you. Davide

KoolReport commented on Mar 10, 2019

Hi davide,

Thank you very much for using our beloved KoolReport.

For your question, may I know the sample value in column worktime?

davide commented on Mar 16, 2019

The "worktime" is a "H:i" format or it could be expressed in seconds.

davide commented on Mar 22, 2019

Hi again, please can you help me?

KoolReport commented on Mar 23, 2019

Sorry for my late reply, here is the documentation of the calculate. Basically if your worktime is the number of seconds then you can use like above like this:

"calculate"=>array(
    "{sumTime}"=>array("sum","worktime"),
),
"bottom"=>function($calculated_results){
    //In here you can get $calculated_results["{sumTime}"] in number
    //You convert it to the  H:i here.
    // Later you return the text
    return "Sum time:". $calculated_results["{sumTime}"];
},

As you can see that, we do the conversion in the template "bottom", there is another template is the "top".

Second option is that you may define a function for {sumTime}

"calculate"=>array(
    "{sumTime}"=>function($store){
        //You can return the sum of working time here or you can get the sum then further formatting the sum and return.
        return $store->sum("worktime");
    },
),

It work the same, however you may format the sum of working time here to format "H:i" and return it. The result can be used in "bottom" and "top" template immediately.

Hope that my answer helps. Let me know if you need further assistance.

davide commented on Mar 25, 2019

Thank you for your help. if the "worktime" was already in "h:i:s" how would the sum code be?

Regards

KoolReport commented on Mar 25, 2019

It should be like this:

...
"calculate"=>array(
    "{sumTime}"=>function($store){
        $sum = 0;
        foreach ($store as $row) {
            list($h,$i,$s) = explode(":", $row["worktime"]);
            $sum += $h*3600+$i*60+$s;
        }
        $h = floor($sum/3600);
        $i = floor(($sum - $h*3600)/60);
        $s = $sum - $h*3600 - $i*60;
        return (($h<10)?"0$h":$h).":".(($i<10)?"0$i":$i).":".(($s<10)?"0$s":$s);
    },
),
"bottom"=>"Total worktime: <b>{sumTime}</b>",
...
davide commented on Mar 31, 2019

It works. There was a warning I solved with:

if (is_numeric($h) && is_numeric($i) && is_numeric($s)){
     $sum += $h*3600+$i*60+$s;
}

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
help needed

None