KoolReport's Forum

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

How to calculate Time difference ( elapsed time ) in KoolReport #2993

Open AhmedHaroon opened this topic on on Feb 20, 2023 - 3 comments

AhmedHaroon commented on Feb 20, 2023

I have order's start time and completed time as datetime, now what if i want to calculate time difference ( end time - start time ) of 2 datetime type fields to show time took to complete an order. the format of the 2 fields are same as below:

order_started_at : 2023-01-09 15:03:47

order_completed_at : 2023-01-09 15:06:08

in my MYSQL db, i have created a View and the column added to calculate time difference as:

TIMEDIFF(o.order_completed_at,o.order_started_at)

it returns time difference / elapsed which is fine enough: 00:02:21

i just want to know about how to in KoolReport when i have to calculate within a Report?

regards

KoolReport commented on Feb 22, 2023

Well, you may use CalculatedColumn to create time diff:

->pipe(new CalculatedColumn([
    "time_diff"=>function($row) {
        $start_timestamp = strtotime($row["order_started_at"]);
        $end_timestamp = strtotime($row["order_completed_at"]);
        $time_diff = $end_timestamp - $start_timestamp;

        // Convert time difference to hours, minutes, and seconds
        $hours = floor($time_diff / 3600);
        $minutes = floor(($time_diff % 3600) / 60);
        $seconds = $time_diff % 60;

        // Format the time difference string
        $time_diff_str = sprintf("%02d:%02d:%02d", $hours, $minutes, $seconds);

        return $time_diff_str;
    }
]))

Let us know if you need further assistance

AhmedHaroon commented on Feb 22, 2023

thanks @KoolReport for help, will check this and confirm. regards

AhmedHaroon commented on Feb 24, 2023

@KoolReport

my feedback, its working fine. thanks again to help us.

regards

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
solved

None