19.05.2011, 00:23
Here are the 2 functions I wrote in PHP:
I have made an attempt at converting it to pawn (not tested but should work!):
Code:
function diff($int1, $int2) { if ($int1 > $int2) { return $int1 - $int2; } return $int2 - $int1; } function timec($timestamp, $compare = null) { if ($compare === null) { $compare = time(); } $diff = diff($timestamp, $compare); if ($diff < 60) { return '< 1 minute'; } else if ($diff < 3600) { $num = floor($diff / 60).' minute'; } else if ($diff < 86400) { $num = floor($diff / 60 / 60).' hour'; } else if ($diff < 2592000) { $num = floor($diff / 60 / 60 / 24).' day'; } else if ($diff < 31536000) { // 31536000 = 1 year $num = floor($diff / 60 / 60 / 24 / 30).' month'; } else { return 'A very very long time'; } if ($num > 1) { return $num.'s'; } return $num; }
pawn Code:
stock timec(timestamp, compare = -1) {
if (compare == -1) {
compare = gettime();
}
new
n,
// on the following line, I have removed the need for the diff() function.
// if you want to use the diff() function in pawn, replace the following with:
// Float:d = diff(timestamp, compare),
Float:d = (timestamp > compare) ? timestamp - compare : compare - timestamp,
returnstr[32];
if (d < 60) {
format(returnstr, sizeof(returnstr), '< 1 minute');
return returnstr;
} else if (d < 3600) { // 3600 = 1 hour
n = floatround(floatdiv(d, 60.0), floatround_floor);
format(returnstr, sizeof(returnstr), 'minute');
} else if (d < 86400) { // 86400 = 1 day
n = floatround(floatdiv(d, 3600.0), floatround_floor);
format(returnstr, sizeof(returnstr), 'hour');
} else if (d < 2592000) { // 2592000 = 1 month
n = floatround(floatdiv(d, 86400.0), floatround_floor);
format(returnstr, sizeof(returnstr), 'day');
} else if (d < 31536000) { // 31536000 = 1 year
n = floatround(floatdiv(d, 2592000.0), floatround_floor);
format(returnstr, sizeof(returnstr), 'month');
} else {
n = floatround(floatdiv(d, 31536000.0), floatround_floor);
format(returnstr, sizeof(returnstr), 'year');
}
if (n == 1) {
format(returnstr, sizeof(returnstr), '1 %s', returnstr);
} else {
format(returnstr, sizeof(returnstr), '%d %ss', n, returnstr);
}
return returnstr;
}