fix: refactor visitor statistics calculation and layout

master
Manop Kongoon 2026-02-27 07:05:47 +07:00
parent 35a9c7f61d
commit 59f9c24ce3
3 changed files with 32 additions and 13 deletions

View File

@ -1 +1,3 @@
- Reduced font size of the statistics table in the footer for better alignment. - Reduced font size of the statistics table in the footer for better alignment.
- Refactored visitor statistics calculation logic for better accuracy and performance.
- Fixed incorrect aggregation in monthly and yearly statistics.

View File

@ -129,37 +129,54 @@ class AbtComponent
public function getCounterToday() public function getCounterToday()
{ {
$counter = Counter::find()->where(['date_visit' => date('Y-m-d')])->one(); $counter = Counter::find()->where(['date_visit' => date('Y-m-d')])->one();
return isset($counter) ? $counter->visit : 0; return isset($counter) ? (int)$counter->visit : 0;
} }
public function getCounterYesterday() public function getCounterYesterday()
{ {
$counter = Counter::find()->where(['date_visit' => date('Y-m-d', strtotime('Yesterday'))])->one(); $counter = Counter::find()->where(['date_visit' => date('Y-m-d', strtotime('Yesterday'))])->one();
return isset($counter) ? $counter->visit : 0; return isset($counter) ? (int)$counter->visit : 0;
} }
public function getCounterThisMonth() public function getCounterThisMonth()
{ {
$counter = Counter::find()->where(['MONTH(date_visit)' => date('m')])->sum('visit'); $firstDay = date('Y-m-01');
return isset($counter) ? $counter : 0; $lastDay = date('Y-m-t');
return (int)Counter::find()
->where(['between', 'date_visit', $firstDay, $lastDay])
->sum('visit');
} }
public function getCounterLastMonth() public function getCounterLastMonth()
{ {
$counter = Counter::find()->where(['MONTH(date_visit)' => date('m', strtotime('-1 month'))])->one(); $firstDay = date('Y-m-01', strtotime('-1 month'));
return isset($counter) ? $counter->visit : 0; $lastDay = date('Y-m-t', strtotime('-1 month'));
return (int)Counter::find()
->where(['between', 'date_visit', $firstDay, $lastDay])
->sum('visit');
} }
public function getCounterThisYear() public function getCounterThisYear()
{ {
$counter = Counter::find()->where(['YEAR(date_visit)' => date('Y')])->sum('visit'); $firstDay = date('Y-01-01');
return isset($counter) ? $counter : 0; $lastDay = date('Y-12-31');
return (int)Counter::find()
->where(['between', 'date_visit', $firstDay, $lastDay])
->sum('visit');
} }
public function getCounterLastYear() public function getCounterLastYear()
{ {
$counter = Counter::find()->where(['YEAR(date_visit)' => date('Y', strtotime('-1 year'))])->one(); $firstDay = date('Y-01-01', strtotime('-1 year'));
return isset($counter) ? $counter->visit : 0; $lastDay = date('Y-12-31', strtotime('-1 year'));
return (int)Counter::find()
->where(['between', 'date_visit', $firstDay, $lastDay])
->sum('visit');
}
public function getCounterTotal()
{
return (int)Counter::find()->sum('visit');
} }
} }

View File

@ -1132,7 +1132,7 @@ $assets = Yii::$app->assetManager->getPublishedUrl('@frontend/themes/mali/assets
</tr> </tr>
<tr> <tr>
<td>รวมทั้งหมด</td> <td>รวมทั้งหมด</td>
<td class="text-right"><?= number_format(Yii::$app->abt->getCounterLastYear(), 0) ?></td> <td class="text-right"><?= number_format(Yii::$app->abt->getCounterTotal(), 0) ?></td>
</tr> </tr>
</table> </table>
</div> </div>