blob: dc8b54fe0b9c7d527903d038201f50caf8385d28 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?php
function format_timestamp(int $timestamp)
{
$days = (int) floor($timestamp / (60.0 * 60.0 * 24.0));
$years = (int) floor($days / 365);
$hours = (int) floor(round($timestamp / (60 * 60)) % 24);
$minutes = (int) floor(round($timestamp % (60 * 60)) / 60);
$seconds = (int) floor($timestamp % 60);
if ($years == 0 && $days == 0 && $hours == 0 && $minutes == 0) {
return "$seconds second" . ($seconds > 1 ? "s" : "");
} else if ($years == 0 && $days == 0 && $hours == 0) {
return "$minutes minute" . ($minutes > 1 ? "s" : "");
} else if ($years == 0 && $days == 0) {
return "$hours hour" . ($hours > 1 ? "s" : "");
} else if ($years == 0) {
return "$days day" . ($days > 1 ? "s" : "");
} else {
return "$years year" . ($years > 1 ? "s" : "");
}
}
|