Changeset 2656

Show
Ignore:
Timestamp:
05/07/08 11:59:30 (3 months ago)
Author:
Shadowhand
Message:

Follow up to r2655, it's a good thing to have working methods.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/system/helpers/date.php

    r2655 r2656  
    355355        } 
    356356 
     357        /** 
     358         * Returns time difference between two timestamps, in the format: 
     359         * N year, N months, N weeks, N days, N hours, N minutes, and N seconds ago 
     360         * 
     361         * @param   integer       timestamp 
     362         * @param   integer       timestamp, defaults to the current time 
     363         * @param   string        formatting string 
     364         * @return  string 
     365         */ 
    357366        public static function timespan_string($time1, $time2 = NULL, $output = 'years,months,weeks,days,hours,minutes,seconds') 
    358367        { 
    359368                if ($difference = date::timespan($time1, $time2, $output) AND is_array($difference)) 
    360369                { 
    361                         $key = end($difference); 
    362                         $key = key($difference); 
    363  
    364                         echo Kohana::debug($key, $difference);exit; 
     370                        // Determine the key of the last item in the array 
     371                        $last = end($difference); 
     372                        $last = key($difference); 
    365373 
    366374                        $span = array(); 
    367375                        foreach ($difference as $name => $amount) 
    368376                        { 
    369                                 if ($amount === 1) 
     377                                if ($name !== $last AND $amount === 0) 
     378                                { 
     379                                        // Skip empty amounts 
     380                                        continue; 
     381                                } 
     382                                elseif ($amount === 1) 
    370383                                { 
    371384                                        // Make the name singualr 
     
    373386                                } 
    374387 
     388                                if ($name === $last) 
     389                                { 
     390                                        // Add "and" 
     391                                        $amount = 'and '.$amount; 
     392                                } 
     393 
     394                                // Add the amount to the span 
    375395                                $span[] = $amount.' '.$name; 
    376396                        } 
    377397 
    378                         $span = array_splice() 
    379  
    380                         $span = trim($span).' ago'; 
    381                 } 
     398                        $difference = implode(', ', $span).' ago'; 
     399                } 
     400 
     401                return $difference; 
    382402        } 
    383403