如何按时间戳计算在我的应用上花费的时间

如何按时间戳计算在我的应用上花费的时间

问题描述:

当用户进入应用程序时,我有一个列时间戳,当用户离开应用程序时有另一列时间戳.我想计算在应用程序上花费的时间:sum(timestamp_exit) - sum (timestamp_enter) .

i have one column timestamp when user enters the app and another column when user leaves the app . i want to calculate the time spent on the app : sum(timestamp_exit) - sum (timestamp_enter) .

现在我试图纠正当前的查询:

right now i've tried to right the current query :

select (SUM(unix_timestamp(`created_time_enter`))) as enter , (SUM(unix_timestamp(`created_time_exit`))) as exit
FROM `my_table` 

但是我得到了很大的数字,我不知道这是否是正确的方法.有什么建议吗?

but i get large numbers and i don't know if it's the correct way. any suggestion?

您可以使用 timeDiff 函数计算:

You could calculate this using the timeDiff function:

times = array();

foreach ($result as $row){
    // convert to unix timestamps
    $firstTime=strtotime($firstTime);
    $lastTime=strtotime($lastTime);

    // perform subtraction to get the difference (in seconds) between times
    $timeDiff=$lastTime-$firstTime;
    $times[] = $timeDiff;
    echo(secondsToTime($timeDiff));
    # 18 days, 23 hours, 41 minutes and 7 seconds
}

echo(secondsToTime(array_sum($times)));
#total of all times