Rannie Ollit
Rannie Ollit

Reputation: 160

Get average date in a selected query in Laravel

Is it possible to compute the average time of a user in a query?

Like:

reportedDate         userID
------------         ------
2018-03-17 00:27:15     1
2018-03-17 00:32:28     1

Upvotes: 0

Views: 1195

Answers (2)

Korri
Korri

Reputation: 657

With eloquent, you can use User::avg('reportedDate') or User::where(...)->avg('reportedDate') if you need a condition.

And if you want to group by user:

 User::groupBy('userId')->avg('reportedDate');

Upvotes: 1

John Ellmore
John Ellmore

Reputation: 2229

Something like

SELECT
    `user_id`,
    DATE_FORMAT(FROM_UNIXTIME(AVG(UNIX_TIMESTAMP(`reportedDate `))), '%e %b %Y') as `averageReportedDate`
FROM `users`
GROUP BY `user_id`

should work.

Upvotes: 1

Related Questions