Closed Thread Icon

Topic awaiting preservation: Relative Date Function (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=29501" title="Pages that link to Topic awaiting preservation: Relative Date Function (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Relative Date Function <span class="small">(Page 1 of 1)</span>\

 
rukuartic
Bipolar (III) Inmate

From: Underneath a mountain of blankets.
Insane since: Jan 2007

posted posted 09-04-2007 21:12

No... I didn't disappear. I've just been lurking. Wrote it this evening... was fairly proud of it. Comments? Oo;

code:
<?

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
    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);
    }
}
?>



Who's on First?

poi
Paranoid (IV) Inmate

From: Norway
Insane since: Jun 2002

posted posted 09-04-2007 23:42

Similar function in JS:

code:
function diff( hay, ref )
{
	var	dif	= Math.round( (hay.valueOf()-(ref||new Date()))/1000 )
		,h	=
		{
			'year'		:60*60*24*365.25
			,'week'		:60*60*24*7
			,'day'		:60*60*24
			,'hour'		:60*60
			,'minute'	:60
			,'second'	:1
		}
		,mod


	if( dif )
		for( var k in h )
			if( 'nmber'==typeof(h[k]) && (mod=Math.floor(Math.abs(dif)/h[k])) )
				return (dif>0?'in ':'')+ mod +' '+ k +(mod-1?'s':'')+(dif<0?' ago':'');

	return 'now';
}





(Edited by poi on 09-04-2007 23:50)

zavaboy
Paranoid (IV) Inmate

From: f(x)
Insane since: Jun 2004

posted posted 09-05-2007 00:41

Here's yet another similar function in Perl:

code:
# The difference in seconds is passed to the function.
# Counts up to days, not weeks, months, or years.
sub convert_sec {
  $seconds = shift; $ret = ""; $days = $hours = $minutes = 0;
  if ($seconds >= 60){ $minutes = floor($seconds/60); $seconds -= $minutes*60; }
  if ($minutes >= 60){ $hours = floor($minutes/60); $minutes -= $hours*60; }
  if ($hours >= 24){ $days = floor($hours/24); $hours -= $days*24; }
  if ($days){ $ret .= $days."d "; }
  if ($hours){ $ret .= $hours."h "; }
  if ($minutes){ $ret .= $minutes."m "; }
  if ($seconds){ $ret .= $seconds."s"; }
  $ret =~ s/(^\s+|\s+$)//g;
  return $ret;
}



rukuartic
Bipolar (III) Inmate

From: Curled up with a ... ...warm laptop?
Insane since: Jan 2007

posted posted 09-05-2007 00:42

D: And that'll get the user's computer to run it won't it... thus saving server CPU and...

...and he did it in fewer lines and more obfuscated code too...

<? include("signature.php"); ?>

« BackwardsOnwards »

Show Forum Drop Down Menu