diff options
| -rw-r--r-- | lib/utils.php | 32 | ||||
| -rw-r--r-- | public/index.php | 4 |
2 files changed, 25 insertions, 11 deletions
diff --git a/lib/utils.php b/lib/utils.php index 6b4b29e..085ed05 100644 --- a/lib/utils.php +++ b/lib/utils.php @@ -24,21 +24,35 @@ function generate_random_char_sequence(array $chars, int $length): string return $o; } -function format_timestamp(int $timestamp_secs) +function format_timestamp(string|DateTime $datetime) { - $days = (int) floor($timestamp_secs / (60.0 * 60.0 * 24.0)); - $hours = (int) floor(round($timestamp_secs / (60 * 60)) % 24); - $minutes = (int) floor(round($timestamp_secs % (60 * 60)) / 60); - $seconds = (int) floor($timestamp_secs % 60); + $dt = $datetime instanceof DateTime ? $datetime : new DateTime($datetime); + $now = new DateTime(); - if ($days == 0 && $hours == 0 && $minutes == 0) { + $diff = match ($dt->getTimestamp() < $now->getTimestamp()) { + true => $dt->diff($now), + false => $now->diff($dt) + }; + + $seconds = $diff->s; + $days = $diff->d; + $minutes = $diff->i; + $hours = $diff->h; + $years = $diff->y; + $months = $diff->m; + + if ($years == 0 && $months == 0 && $days == 0 && $hours == 0 && $minutes == 0) { return "$seconds second" . ($seconds > 1 ? "s" : ""); - } else if ($days == 0 && $hours == 0) { + } else if ($years == 0 && $months == 0 && $days == 0 && $hours == 0) { return "$minutes minute" . ($minutes > 1 ? "s" : ""); - } else if ($days == 0) { + } else if ($years == 0 && $months == 0 && $days == 0) { return "$hours hour" . ($hours > 1 ? "s" : ""); - } else { + } else if ($years == 0 && $months == 0) { return "$days day" . ($days > 1 ? "s" : ""); + } else if ($years == 0) { + return "$months month" . ($months > 1 ? "s" : ""); + } else { + return "$years year" . ($years > 1 ? "s" : ""); } } diff --git a/public/index.php b/public/index.php index 44b97bf..0493cc4 100644 --- a/public/index.php +++ b/public/index.php @@ -99,7 +99,7 @@ if (FILE_CATALOG_FANCY_VIEW && $file_id) { } if (isset($file['duration'])) { - array_push($file['resolution'], format_timestamp($file['duration'])); + array_push($file['resolution'], format_timestamp(new DateTime()->setTimestamp(time() + $file['duration']))); } if (isset($file['line_count'])) { @@ -153,7 +153,7 @@ $privacy_exists = is_file($_SERVER['DOCUMENT_ROOT'] . '/static/PRIVACY.txt'); <?php endif; ?> </div> <div class="row gap-8 grow align-bottom"> - <p>Uploaded <?= format_timestamp(time() - strtotime($file['uploaded_at'])) ?> ago</p> + <p>Uploaded <?= format_timestamp($file['uploaded_at']) ?> ago</p> </div> <div class="row gap-8 grow align-bottom"> <?php if (FILE_COUNT_VIEWS && isset($file['views'])): ?> |
