Relative Dates code written 13 years ago

Sennir.co.uk

If you’re a hardcore sennir reader, you might remember me being very proud of my dodgy relative dates code I wrote over a year ago. It turns out that code was broken, and has been ever since I wrote it.

This is where you come in. Well Hopefully.


      <?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.
       * If "$to" is provided, then we should return the time "until" or "from"
       * that time. Using the style "1 day later" or "3 hours ago".
       */
      
function datetimeToRelative($datetime$to=0)
      {
        if(
$to === 0)/* Get the current time + DST */
            
$current_time time();
        else
            
$current_time strtotime($to);
        
/* 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' 'later');

        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 ?
        
{
            if(
$relative_time 0)
                return 
'yesterday';
            else
                return 
'1 day '.$when;
        }
        elseif (
$abs_relative_time 86400// Less than a day ago 
            
return ($hours floor($abs_relative_time 3600)).' '.($hours 'hours' 'hour').' '.$when;   

      
//    echo "abs relative time is:" .$abs_relative_time;
        /* 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);

      
//    echo "now abs relative time is:" .$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;

      }
      
?>

There’s the code.. can anybody fix it?

In the meantime, anywhere a relative date is used, it’ll just be the datetime stamp.

There’s cookies on offer!

Update

Looks like there was only a small error in the code, and so, hopefully, it’s fixed. If any other weirdiosities occur, please let me know!

← previous entry | next entry → Sun 16th Jul 2006 - 19:54 | no comments | tagged with Sennir News, Programming

 There are no responses to this entry

Do you want to say something? Use the form below to add your thoughts.

Note if you submit your comments, they'll appear here! yes, right below this. Try and make them relevant eh?

Leave a reply

Name: displayed next to your comment
Email: will not be displayed
URL: to link back to your site
Comments: Markdown syntax allowed

Notes on leaving replies: