From 59f9c24ce3db940a2e736a8939176b2d25da830f Mon Sep 17 00:00:00 2001 From: Manop Kongoon Date: Fri, 27 Feb 2026 07:05:47 +0700 Subject: [PATCH] fix: refactor visitor statistics calculation and layout --- CHANGELOG.md | 2 + common/components/AbtComponent.php | 41 +++++++++++++++------ frontend/themes/mali/views/layouts/main.php | 2 +- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 03716bff..66bd9ebe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1 +1,3 @@ - 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. diff --git a/common/components/AbtComponent.php b/common/components/AbtComponent.php index 59f0e843..20a7fe52 100755 --- a/common/components/AbtComponent.php +++ b/common/components/AbtComponent.php @@ -129,37 +129,54 @@ class AbtComponent public function getCounterToday() { - $counter = Counter::find()->where(['date_visit' => date('Y-m-d')])->one(); - return isset($counter) ? $counter->visit : 0; + $counter = Counter::find()->where(['date_visit' => date('Y-m-d')])->one(); + return isset($counter) ? (int)$counter->visit : 0; } public function getCounterYesterday() { - $counter = Counter::find()->where(['date_visit' => date('Y-m-d', strtotime('Yesterday'))])->one(); - return isset($counter) ? $counter->visit : 0; + $counter = Counter::find()->where(['date_visit' => date('Y-m-d', strtotime('Yesterday'))])->one(); + return isset($counter) ? (int)$counter->visit : 0; } public function getCounterThisMonth() { - $counter = Counter::find()->where(['MONTH(date_visit)' => date('m')])->sum('visit'); - return isset($counter) ? $counter : 0; + $firstDay = date('Y-m-01'); + $lastDay = date('Y-m-t'); + return (int)Counter::find() + ->where(['between', 'date_visit', $firstDay, $lastDay]) + ->sum('visit'); } public function getCounterLastMonth() { - $counter = Counter::find()->where(['MONTH(date_visit)' => date('m', strtotime('-1 month'))])->one(); - return isset($counter) ? $counter->visit : 0; + $firstDay = date('Y-m-01', strtotime('-1 month')); + $lastDay = date('Y-m-t', strtotime('-1 month')); + return (int)Counter::find() + ->where(['between', 'date_visit', $firstDay, $lastDay]) + ->sum('visit'); } public function getCounterThisYear() { - $counter = Counter::find()->where(['YEAR(date_visit)' => date('Y')])->sum('visit'); - return isset($counter) ? $counter : 0; + $firstDay = date('Y-01-01'); + $lastDay = date('Y-12-31'); + return (int)Counter::find() + ->where(['between', 'date_visit', $firstDay, $lastDay]) + ->sum('visit'); } public function getCounterLastYear() { - $counter = Counter::find()->where(['YEAR(date_visit)' => date('Y', strtotime('-1 year'))])->one(); - return isset($counter) ? $counter->visit : 0; + $firstDay = date('Y-01-01', strtotime('-1 year')); + $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'); } } diff --git a/frontend/themes/mali/views/layouts/main.php b/frontend/themes/mali/views/layouts/main.php index 2c5d721f..da2b6c48 100644 --- a/frontend/themes/mali/views/layouts/main.php +++ b/frontend/themes/mali/views/layouts/main.php @@ -1132,7 +1132,7 @@ $assets = Yii::$app->assetManager->getPublishedUrl('@frontend/themes/mali/assets รวมทั้งหมด - abt->getCounterLastYear(), 0) ?> + abt->getCounterTotal(), 0) ?>