I wrote this for a small web app I was writing... I lost the previous one I had (which was sad... it had a neat thing where you could define the format...) but this one is more efficient, I think...
Anyone have a few ideas how to make it better?
<?
function reltime($old_time, $cur_time = -1)
{
// Code by RukuArtic, 2007
// Bunch of defines for my code...
define('YEAR', 1);
define('MONTH', 2);
define('WEEK', 3);
define('DAY', 4);
define('HOUR', 5);
define('MIN', 6);
define('SECOND', 7);
define('WORD_YEAR', " year");
define('WORD_MONTH', " month");
define('WORD_WEEK', " week");
define('WORD_DAY', " day");
define('WORD_HOUR', " hour");
define('WORD_MIN', " minute");
define('WORD_SEC', " second");
// This just makes pluralizing easier.
define('P', "s");
define('SEC_YEAR', 31556926);
define('SEC_MONTH', 2629744);
define('SEC_WEEK', 604800);
define('SEC_DAY', 86400);
define('SEC_HOUR', 3600);
define('SEC_MIN', 60);
$format = "%t ago";
// If they didn't set a time to compare it to, use the current time.
if($cur_time == -1)
$cur_time = time();
// Calculate the number of seconds between times, compensate for
// times in the future.
$diff = $cur_time - $old_time;
if($diff < 0)
{
$format = "in %t";
$diff = abs($diff);
}
// Heehee. This one's hard to get, but prevents "0 seconds ago"
if($diff == 0)
return "now";
// Calculate the number of years... etc, keep as integer.
// I'm sure this could be more efficient if we break as soon as we
// find a number that's not zero...
$num[YEAR] = (int)($diff / SEC_YEAR);
$diff = $diff % SEC_YEAR;
$num[MONTH] = (int)($diff / SEC_MONTH);
$diff = $diff % SEC_MONTH;
$num[WEEK] = (int)($diff / SEC_WEEK);
$diff = $diff % SEC_WEEK;
$num[DAY] = (int)($diff / SEC_DAY);
$diff = $diff % SEC_DAY;
$num[HOUR] = (int)($diff / SEC_HOUR);
$diff = $diff % SEC_HOUR;
$num[MINUTE] = (int)($diff / SEC_MIN);
$diff = $diff % SEC_MIN;
$num[SECOND] = $diff;
// Find the biggest value -- ie: 1 year > 11 months
$type = SECOND;
for($i = SECOND; $i >= YEAR; $i--)
if($num[$i] > 0)
$type = $i;
// Assume plural, unless the number is 1.
$plural = true;
if($num[$type] == 1)
$plural = false;
// And return it. If you don't know how this code works, read this article
// about the Ternary Operator (?:). http://en.wikipedia.org/wiki/%3F:
switch($type)
{
case YEAR:
return str_replace('%t', ($plural ? $num[YEAR] . WORD_YEAR . P : $num[YEAR] . WORD_YEAR), $format);
case MONTH:
return str_replace('%t', ($plural ? $num[MONTH] . WORD_MONTH . P : $num[MONTH] . WORD_MONTH), $format);
case WEEK:
return str_replace('%t', ($plural ? $num[WEEK] . WORD_WEEK . P : $num[WEEK] . WORD_WEEK), $format);
case DAY:
return str_replace('%t', ($plural ? $num[DAY] . WORD_DAY . P : $num[DAY] . WORD_DAY), $format);
case HOUR:
return str_replace('%t', ($plural ? $num[HOUR] . WORD_HOUR . P : $num[HOUR] . WORD_HOUR), $format);
case MINUTE:
return str_replace('%t', ($plural ? $num[MINUTE] . WORD_MINUTE . P : $num[MINUTE] . WORD_MINUTE), $format);
case SECOND:
return str_replace('%t', ($plural ? $num[SECOND] . WORD_SECOND . P : $num[SECOND] . WORD_SECOND), $format);
}
}
?>