user1012181
user1012181

Reputation: 8726

Calculate the total time in Seconds

I have a field in my table which has the time for a particular action. The entries are like 00:01:29
00:02:12
00:00:45 etc...

Now I want to get the sum of all these time in seconds.

I tried this:
$time_for_correct_ans = Testlog::where('token', $exists->token)->where('correct', 1)->sum('answering_time');

But it took 00:01:29 as 129 seconds which is wrong. Is there any better way to do it?

I'm using Laravel and MySQL.

Upvotes: 0

Views: 254

Answers (1)

lukasgeiter
lukasgeiter

Reputation: 152860

You can use MySQLs TIME_TO_SEC:

$time_for_correct_ans = Testlog::selectRaw('SUM(TIME_TO_SEC(answering_time)) AS total')
                               ->where('token', $exists->token)
                               ->where('correct', 1)
                               ->pluck('total');

Upvotes: 2

Related Questions