Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

strangequirks's avatar

Laravel timestamp sum issue

Hello

I want to sum the daily working hours of a user and i have the time for checkin and checkout in a time column

$sql=" SELECT User_id, date, min(time) AS checkedin, max(time) AS checkedout,( (TIME_TO_SEC(TIMEDIFF(max(time), min(time))) / 60) / 60) difference
            FROM profile WHERE 1 GROUP BY User_id, date";
            $previousdata = DB::select($sql);

I get this array from the sql query that i have used and i want to add the difference that i get for everyday for a month's data. not to worry abt the date format, i am from nepal and am using the nepali date.

Array
(
    [0] => stdClass Object
        (
            [User_id] => 1
            [date] => 2076-02-06
            [checkedin] => 12:11:40
            [checkedout] => 19:11:43
            [difference] => 7.00083333
        )

    [1] => stdClass Object
        (
            [User_id] => 1
            [date] => 2076-02-08
            [checkedin] => 12:15:40
            [checkedout] => 15:15:48
            [difference] => 3.00222222
        )

)
0 likes
2 replies
tisuchi's avatar
tisuchi
Best Answer
Level 70

@strangequirks

Try-

array_sum(array_column($arr,'difference'));

For example-

$arr = [
    0 => (object) [
        "User_id"    => "1",
        "date"       => "2076-02-06",
        "checkedin"  => "12:11:40",
        "checkedout" => "19:11:43",
        "difference" => "7.00083333",
    ],
    1 => (object) [
        "User_id"    => "1",
        "date"       => "2076-02-08",
        "checkedin"  => "12:15:40",
        "checkedout" => "15:15:48",
        "difference" => "3.00222222",
    ],

];

$temp = array_sum(array_column($arr, 'difference'));

echo $temp;
3 likes

Please or to participate in this conversation.