Skip to content

Commit c9e364b

Browse files
jack9603301erikdubbelboer
authored andcommitted
Correction time shows that time is more scientific (erikdubbelboer#130)
Improve time formatting
1 parent 2cf0f77 commit c9e364b

File tree

2 files changed

+30
-27
lines changed

2 files changed

+30
-27
lines changed

includes/functions.inc.php

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,30 @@ function input_convert($str) {
2626
}
2727

2828

29-
function format_ago($time, $ago = false) {
29+
function format_time($time) {
3030
$minute = 60;
3131
$hour = $minute * 60;
3232
$day = $hour * 24;
3333

3434
$when = $time;
3535

36-
if ($when >= 0)
37-
$suffix = 'ago';
38-
else {
39-
$when = -$when;
40-
$suffix = 'in the future';
41-
}
42-
4336
if ($when > $day) {
44-
$when = round($when / $day);
45-
$what = 'day';
37+
$tmpday = floor($when / $day);
38+
$tmphour = floor(($when / $hour) - (24*$tmpday));
39+
$tmpminute = floor(($when / $minute) - (24*60*$tmpday) - ($tmphour * 60));
40+
$tmpsec = floor($when - (24*60*60*$tmpday) - ($tmphour * 60 * 60) - ($tmpminute * 60));
41+
return sprintf("%d day%s %d hour%s %d min%s %d sec%s",$tmpday,($tmpday != 1) ? 's' : '',$tmphour,($tmphour != 1) ? 's' : '',$tmpminute,($tmpminute != 1) ? 's' : '',$tmpsec,($tmpsec != 1) ? 's' : '');
4642
} else if ($when > $hour) {
47-
$when = round($when / $hour);
48-
$what = 'hour';
43+
$tmphour = floor($when / $hour);
44+
$tmpminute = floor(($when / $minute) - ($tmphour * 60));
45+
$tmpsec = floor($when - ($tmphour * 60 * 60) - ($tmpminute * 60));
46+
return sprintf("%d hour%s %d min%s %d sec%s",$tmphour,($tmphour != 1) ? 's' : '',$tmpminute,($tmpminute != 1) ? 's' : '',$tmpsec,($tmpsec != 1) ? 's' : '');
4947
} else if ($when > $minute) {
50-
$when = round($when / $minute);
51-
$what = 'minute';
52-
} else {
53-
$what = 'second';
54-
}
55-
56-
if ($when != 1) $what .= 's';
57-
58-
if ($ago) {
59-
return "$when $what $suffix";
48+
$tmpminute = floor($when / $minute);
49+
$tmpsec = floor($when - ($tmpminute * 60));
50+
return sprintf("%d min%s %d sec%s",$tmpminute,($tmpminute != 1) ? 's' : '',$tmpsec,($tmpsec != 1) ? 's' : '');
6051
} else {
61-
return "$when $what";
52+
return sprintf("%d sec%s",$when,($when != 1) ? 's' : '');
6253
}
6354
}
6455

@@ -76,7 +67,7 @@ function format_size($size) {
7667

7768
function format_ttl($seconds) {
7869
if ($seconds > 60) {
79-
return sprintf('%d (%s)', $seconds, format_ago($seconds));
70+
return sprintf('%d (%s)', $seconds, format_time($seconds));
8071
} else {
8172
return $seconds;
8273
}

overview.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,21 @@
8383

8484
<tr><td><div>Memory used:</div></td><td><div><?php echo format_size($info[$i]['Memory']['used_memory'])?></div></td></tr>
8585

86-
<tr><td><div>Uptime:</div></td><td><div><?php echo format_ago($info[$i]['Server']['uptime_in_seconds'])?></div></td></tr>
87-
88-
<tr><td><div>Last save:</div></td><td><div><?php if (isset($info[$i]['Persistence']['rdb_last_save_time'])) { echo format_ago(time() - $info[$i]['Persistence']['rdb_last_save_time'], true); } else { echo 'never'; } ?> <a href="save.php?s=<?php echo $i?>"><img src="images/save.png" width="16" height="16" title="Save Now" alt="[S]" class="imgbut"></a></div></td></tr>
86+
<tr><td><div>Uptime:</div></td><td><div><?php echo format_time($info[$i]['Server']['uptime_in_seconds'])?></div></td></tr>
87+
88+
<tr><td><div>Last save:</div></td><td><div>
89+
<?php
90+
if (isset($info[$i]['Persistence']['rdb_last_save_time'])) {
91+
if((time() - $info[$i]['Persistence']['rdb_last_save_time'] ) >= 0) {
92+
echo format_time(time() - $info[$i]['Persistence']['rdb_last_save_time']) . " ago";
93+
} else {
94+
echo format_time(-(time() - $info[$i]['Persistence']['rdb_last_save_time'])) . "in the future";
95+
}
96+
} else {
97+
echo 'never';
98+
}
99+
?>
100+
<a href="save.php?s=<?php echo $i?>"><img src="images/save.png" width="16" height="16" title="Save Now" alt="[S]" class="imgbut"></a></div></td></tr>
89101

90102
</table>
91103
<?php endif; ?>

0 commit comments

Comments
 (0)