Hello. I'm in the middle of coding a small library to use on a blog source I'm making, and there's a function I'm trying to add that gives a user a rough idea of when something happened or will happen.

For example, If someone wants to know how long it's been since a post on, say, June 17th, the function would print "4 months, 2 weeks ago", while an indicator for Christmas would print "1 month, 3 weeks from now".

The function will use the highest two measurements of time. Something that happened ten years ago would be measured with years, then months. A post that was a few hours ago would return hours and minutes. Something around my birthday (Oct 22nd) would return x weeks, y days. Of course, if the difference is small, then it just outputs minutes.

Enough of the explanation though, I should probably just show you the code. (Note: You can't post links when you're a newbie? Wow...)

PHP Code:
<?php

/* date_span ( int $date )
 *
 * Provides a human-readable difference between the current time and $date,
 * which is a UNIX timestamp. Returns a string that indicates the difference
 * between the times.
 * 
 */
function date_span$date ) {
    
// Make sure $date is an integer.
    
if( is_int$date ) ) {
        
$hdiff = array();
        
$now time();
        
$diff $now $date;
        
        
// Decipher whether the date is in the past or the future.
        
if ( $diff ) {
            
$mod_word "from now";
        } else {
            
$mod_word "ago";
        }

        
$diff abs($diff); // Make it an absolute value so math can be done.
        
if ( $diff 31104000 ) {
            
$format "year";
        } else if ( 
$diff 2592000 0) {
            
$format "month";
        } else if ( 
$diff 604800 0) {
            
$format "week";
        } else if ( 
$diff 86400 0) {
            
$format "day";
        } else if ( 
$diff 3600 0) {
            
$format "hour";
        } else {
            
$format "minute";
        }

        switch (
$format) {
            case 
"year":
                
$hdiff[0] = $diff 31104000;
                
$hdiff[1] = ($diff $hdiff[0] * 31104000) / 2592000;
                
printf("%d years, %d months " $mod_word$hdiff[0], $hdiff[1]);
                break;
            case 
"month":
                
$hdiff[0] = $diff 2592000;
                
$hdiff[1] = ($diff $hdiff[0] * 2592000) / 604800;
                
printf("%d months, %d weeks " $mod_word$hdiff[0], $hdiff[1]);
                break;
            case 
"week":
                
$hdiff[0] = $diff 604800;
                
$hdiff[1] = ($diff $hdiff[0] * 604800) / 86400;
                
printf("%d weeks, %d days " $mod_word$hdiff[0], $hdiff[1]);
                break;
            case 
"day":
                
$hdiff[0] = $diff 86400;
                
$hdiff[1] = ($diff $hdiff[0] * 86400) / 3600;
                
printf("%d days, %d hours " $mod_word$hdiff[0], $hdiff[1]);
                break;
            case 
"hour":
                
$hdiff[0] = $diff 3600;
                
$hdiff[1] = ($diff $hdiff[0] * 3600) / 60;
                
printf("%d hours, %d minutes " $mod_word$hdiff[0], $hdiff[1]);
                break;
            case 
"minute":
                
$hdiff $diff 60;
                
printf("%d minutes " $mod_word$hdiff);
        }
    } else {
        print 
"ERROR: date_span()'s argument must be an integer.";
    }
}
?>
I apologize if the design is horrid.. I'm a mediocre coder and tried my best.

I tested the function against two dates:

August 18th, 2004 returned "4 years, 0 months ago". Obviously, this should have returned "4 years 2 months".

August 18th, 2009 returned "0 years, 0 months from now". I'm not quite sure why it did this, since I've made $diff an absolute value to get rid of the negative sign after determining whether it's in the future or the past. It should've guessed that the measurement was months first, then given weeks. The correct answer is "9 months, 2 weeks from now".

If anyone could help me better understand how to do this (and maybe suggest how to improve it for efficiency or readability) I'd greatly appreciate it.