如何从数据库中的所有列计算平均时间。我正在获取特定列的所有记录。因此,我使用了foreach,但随后它仅考虑单个值。您能建议我如何从所有记录中获取平均时间吗?
此外,如果我使用Model :: avg('created_at');然后我得到一个带有日期和时间的长字符串。但是,拆分这些值变得非常困难。
请查看下面的代码。
感谢您的帮助。
$query = $this->pickupModel->select('pickups.published_at', 'pickups.republished_at')->get()->toArray();
if (!empty($query)) {
echo'comess inside<pre>';
print_r($query);
exit;
foreach ($query as $value) {
echo'comess inside<pre>';
print_r($value['published_at']);
exit;
}
}
Output:-----
comess inside
Array
(
[0] => Array
(
[published_at] => 2019-01-30 11:50:12
[republished_at] => 2019-01-30 11:50:12
)
[1] => Array
(
[published_at] => 2019-01-30 11:50:12
[republished_at] => 2019-01-30 11:50:12
)
[2] => Array
(
[published_at] => 2019-01-31 11:50:12
[republished_at] => 2019-01-31 11:50:12
)
[3] => Array
(
[published_at] =>
[republished_at] =>
)
[4] => Array
(
[published_at] =>
[republished_at] =>
)
[5] => Array
(
[published_at] => 2019-01-30 11:50:12
[republished_at] => 2019-01-30 11:50:12
)
[6] => Array
(
[published_at] => 2019-01-30 11:50:12
[republished_at] => 2019-01-30 11:50:12
)
)