From 26ec6de1e3b0129e97b543e4caad35b2a4472af8 Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 26 Nov 2023 11:01:19 +0100 Subject: [PATCH] nice --- web/inc/helpers.php | 32 +++++++++++++++++++++++++++++++- web/pages/smart/controller.php | 5 +++-- web/pages/smart/search.html.php | 2 ++ 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/web/inc/helpers.php b/web/inc/helpers.php index 9e707e7..aa5b6a7 100644 --- a/web/inc/helpers.php +++ b/web/inc/helpers.php @@ -322,4 +322,34 @@ function template($templatefile,$variables=[]) ob_end_clean(); return $pagecontent; -} \ No newline at end of file +} + +function printRelativeTime($timestamp1, $timestamp2) +{ + $diff = abs($timestamp2 - $timestamp1); + + $intervals = array( + 'year' => 31536000, + 'month' => 2592000, + 'week' => 604800, + 'day' => 86400, + 'hour' => 3600, + 'minute' => 60, + 'second' => 1 + ); + + $output = ''; + + foreach ($intervals as $interval => $seconds) { + $count = floor($diff / $seconds); + + if ($count > 0) { + $output .= $count . ' ' . ($count === 1 ? $interval : $interval . 's') . ', '; + $diff -= $count * $seconds; + } + } + + $output = rtrim($output, ', '); + + return $output; +} diff --git a/web/pages/smart/controller.php b/web/pages/smart/controller.php index 98cf240..7fc171a 100644 --- a/web/pages/smart/controller.php +++ b/web/pages/smart/controller.php @@ -23,16 +23,17 @@ class Smart extends Page { $results = []; while($row = $res->fetchArray()) { - $row['event'] = $db->querySingle("SELECT name FROM events WHERE id = ".$row['event']); $row['date'] = $db->querySingle("SELECT date FROM events WHERE id = ".$row['event']); + $row['event'] = $db->querySingle("SELECT name FROM events WHERE id = ".$row['event']); $row['unixtimestamp'] = strtotime($row['date']); $row['run'] = $db->querySingle("SELECT name FROM runs WHERE id = ".$row['run']); + $row['ago'] = printRelativeTime(time(),$row['unixtimestamp']); $results[] = $row; } //sort results by date usort($results, function($a, $b) { - return $a['unixtimestamp'] <=> $b['unixtimestamp']; + return $b['unixtimestamp'] <=> $a['unixtimestamp']; }); $this->set('results', $results); diff --git a/web/pages/smart/search.html.php b/web/pages/smart/search.html.php index e47794f..ba2fbd5 100644 --- a/web/pages/smart/search.html.php +++ b/web/pages/smart/search.html.php @@ -4,6 +4,7 @@ event + Wann wars run rang stnr @@ -22,6 +23,7 @@ +