<?php

/*
 * Takes a datetime, and returns a nice string, saying 
 * datetime of the form "YYYY-MM-DD HH:MM:SS"
 * 2 days ago, 10 minutes ago 1 hour ago, 3 weeks ago 
 * 4 months ago
 * 6 years ago
 * etc.
 */
function datetimeToRelative($datetime)
{
    
/* Get the current time + DST */
    
$current_time time();

    
/* Get the time of the post */
    
$post_time strtotime($datetime);


    
/* Get when yesterday was */
    
$yesterday_time strtotime('12am yesterday');


    
/* Work out the difference between the two dates; should be positive if in past */
    
$relative_time     $current_time $post_time;
    
$abs_relative_time abs($relative_time);

    
$when              = ($relative_time 'ago' 'in the future');

    if (
$abs_relative_time 60// Less than a minute 
        
return $abs_relative_time.' '.($abs_relative_time 'seconds' 'second').' '.$when;

    elseif (
$abs_relative_time 3600// Less than an hour 
        
return ($minutes floor($abs_relative_time 60)).' '.($minutes 'minutes' 'minute').' '.$when;

    elseif (
$post_time $yesterday_time && $post_time $yesterday_time 86400// Was it yesterday ?
        
return 'yesterday';
    elseif (
$abs_relative_time 86400// Less than a day ago 
        
return ($hours floor($abs_relative_time 3600)).' '.($hours 'hours' 'hour').' '.$when;    

    
/* Since we have ruled out hours / mins / secs, now forget about the times etc. */
    
$post_time         strtotime(substr($datetime010).' 00:00:00');

    
$relative_time     $current_time $post_time;
    
$abs_relative_time abs($relative_time);

    if (
$abs_relative_time 604800// Less than a week 
        
return ($days floor($abs_relative_time 86400)).' '.($days 'days' 'day').' '.$when;

    elseif (
$abs_relative_time 2629743// Less than an month
        
return ($weeks floor($abs_relative_time 604800)).' '.($weeks 'weeks' 'week').' '.$when;

    elseif (
$abs_relative_time 31556926// Less than an year
        
return ($months floor($abs_relative_time 2629743)).' '.($months 'months' 'month').' '.$when;

    else
        return (
$years floor($abs_relative_time 31556926)).' '.($years 'years' 'year').' '.$when;

}

?>