Part of the EllisLab Network
   
 
Time Difference Function
Posted: 09 April 2009 04:14 PM   [ Ignore ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  496
Joined  07-16-2008

Well, after looking around the web at all the bad implementations of a simple human time difference function - I came up with this for MicroMVC. It seems to do the job well and I thought I might share it here for other to use as I thought I remembered seeing some questions about this somewhere.

/**
 * Show a human-readable time difference ("10 seconds") 
 *
 * @see                MicroMVC.com
 * @param int        $from_time
 * @param int        $to_time
 * @return string
 */
function time_difference($from_time=0$to_time=null{
    
    
//If not set - use current time
    
if(!$to_time{ $to_timetime(); }
    
    
//timestamp difference
    
$difference round(abs($to_time $from_time));
    
    
//Try seconds first
    
if ($difference <= 60{
        
return $difference' seconds';
    
}
    
    
//Time Types (you can add to this)
    
$times = array(
        
'minute'    => 60,
        
'hour'        => 60,
        
'day'        => 24,
        
'week'        => 7,
        
'month'        => 4,
        
'year'        => 12
    
);
    
    
    
//Try each type of time
    
foreach($times as $type => $value{
    
        
//Find number of minutes
        
$difference round($difference $value);
    
        if (
$difference <= $value{
            
return $difference' '$type. ($difference 's' '');
        
}
    }

I don’t think it’s necessary, but it’s GPL in case anyone is worried about using it.

 Signature 

My Blog, C2D, PHP Videos, CXTags, Super .htaccess, Extra hooks, and MicroMVC

Profile
 
 
Posted: 10 April 2009 07:01 PM   [ Ignore ]   [ # 1 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  727
Joined  08-03-2006

Here is a more accurate time difference function I made:

function time_diff($from$to)
{
    $diff 
abs($from $to);
    
    
$units = array('year' => 31557600'month' => 2635200'week' => 604800'day' => 86400'hour' => 3600'minute' => 60'second' => 1);
    
    
$str '';
    foreach(
$units as $title => $length)
    
{
        
if($d floor($diff $length))
        
{
            $str[] 
$d ' ' $title . ($d 's' '');
            
            
$diff -= $length $d;
        
}
    }
    
    
return implode(' '$str);

BSD license wink

 Signature 

RapidDataMapper: My new ORM, is now released!

IgnitedRecord: Old ORM

MPTtree: A model to handle trees in a database.

YAYParser - Yet Another YAML Parser

Profile
 
 
Posted: 10 April 2009 09:25 PM   [ Ignore ]   [ # 2 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  496
Joined  07-16-2008

And that my friend, is why I share code. wink

 Signature 

My Blog, C2D, PHP Videos, CXTags, Super .htaccess, Extra hooks, and MicroMVC

Profile
 
 
Posted: 11 April 2009 03:58 AM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  455
Joined  03-28-2008

Hey!

Check out this:

http://codeigniter.com/forums/viewthread/106557/

It depends on languagefiles too…

 Signature 

———————————————————————————————————————————-
Imac 27” Core i7 / 12GB RAM
Macbook Pro 15” C2D 2.53Ghz / 4GB RAM / NVIDIA GeForce 9400M + 9600M GT 512MB
iPhone 4 16Gb Black

http://www.rockkarusellen.se

Profile