Part of the EllisLab Network
   
 
Time Difference Function
Posted: 09 April 2009 03:14 PM   [ Ignore ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  494
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 06: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 08:25 PM   [ Ignore ]   [ # 2 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  494
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 02:58 AM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  433
Joined  03-28-2008

Hey!

Check out this:

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

It depends on languagefiles too…

 Signature 

———————————————————————————————————————————-
Imac 24” C2D 3.06Ghz / 4GB RAM / Geforce 8800GS 512MB
Macbook Pro 15” C2D 2.53Ghz / 4GB RAM / NVIDIA GeForce 9400M + 9600M GT 512MB
iPhone 3G 16Gb Black

http://www.rockkarusellen.se

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 148261 Total Logged-in Users: 67
Total Topics: 103041 Total Anonymous Users: 2
Total Replies: 515894 Total Guests: 474
Total Posts: 618935    
Members ( View Memberlist )