FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


My own Time Ago Calculation Script in PHP





imagefree
Few days ago, i developed my own "Time Ago Calculation" Script.
Its a function that expects numeric time stamp and returns its difference from current date (or if the second argument is provided, difference from the second provided time stamp).

Here is the function.

Code:
function time_ago( $datefrom , $dateto = -1 , $append = 'ago')
{
   if( $datefrom <= 0 OR !ctype_digit($datefrom) )
   {
      return false;
   }
   
   if($dateto === -1)
   {
      $dateto = time();
   }
   
   if( !ctype_digit($dateto) )
   {
      return false;
   }
   
   $difference = $dateto - $datefrom;
   
   if($difference < 60)
   {
      $time_ago   = $difference . ' second' . ( $difference > 1 ? 's' : '' ).' '.$append;
      #$interval = 's';
   }
   else if( $difference < 60*60 )
   {
      $ago_seconds   = $difference % 60;
      $ago_seconds   = ( ( $ago_seconds AND $ago_seconds > 1 ) ? ' and '.$ago_seconds.' seconds' : ( $ago_seconds == 1 ? ' and '.$ago_seconds.' second' : '' ) );
      $ago_minutes   = floor( $difference / 60 );
      $ago_minutes   = $ago_minutes . ' minute' . ( $ago_minutes > 1 ? 's' : '' );
      $time_ago      = $ago_minutes.$ago_seconds.' '.$append;
      
   }
   else if ( $difference < 60*60*24 )
   {
      $ago_minutes   = round( $difference / 60 ) % 60 ;
      $ago_minutes   = ( ( $ago_minutes AND $ago_minutes > 1 ) ? ' and ' . $ago_minutes . ' minutes' : ( $ago_minutes == 1 ? ' and ' . $ago_minutes .' minute' : '' ));
      $ago_hours      = floor( $difference / ( 60 * 60 ) );
      $ago_hours      = $ago_hours . ' hour'. ( $ago_hours > 1 ? 's' : '' );
      $time_ago      = $ago_hours.$ago_minutes.' '.$append;
      
   }
   else if ( $difference < 60*60*24*7 )
   {
      $ago_hours      = round( $difference / 3600 ) % 24 ;
      $ago_hours      = ( ( $ago_hours AND $ago_hours > 1 ) ? ' and ' . $ago_hours . ' hours' : ( $ago_hours == 1 ? ' and ' . $ago_hours . ' hour' : '' ));
      $ago_days      = floor( $difference / ( 3600 * 24 ) );
      $ago_days      = $ago_days . ' day' . ($ago_days > 1 ? 's' : '' );
      $time_ago      = $ago_days.$ago_hours.' '.$append;
      
   }
   else if ( $difference < 60*60*24*30 )
   {
      $ago_days      = round( $difference / ( 3600 * 24 ) ) % 7;
      $ago_days      = ( ( $ago_days AND $ago_days > 1 ) ? ' and '.$ago_days.' days' : ( $ago_days == 1 ? ' and '.$ago_days.' day' : '' ));
      $ago_weeks      = floor( $difference / ( 3600 * 24 * 7) );
      $ago_weeks      = $ago_weeks . ' week'. ($ago_weeks > 1 ? 's' : '' );
      $time_ago      = $ago_weeks.$ago_days.' '.$append;
      
   }
   else if ( $difference < 60*60*24*365 )
   {
      $days_diff   = round( $difference / ( 60 * 60 * 24 ) );
      $ago_days   = $days_diff %  30 ;
      $ago_weeks   = round( $ago_days / 7 ) ;
      $ago_weeks   = ( ( $ago_weeks AND $ago_weeks > 1 ) ? ' and '.$ago_weeks.' weeks' : ( $ago_weeks == 1 ? ' and '.$ago_weeks.' week' : '' ) );
      $ago_months   = floor( $days_diff / 30 );
      $ago_months   = $ago_months .' month'. ( $ago_months > 1 ? 's' : '' );
      $time_ago   = $ago_months.$ago_weeks.' '.$append;
      
   }
   else if ( $difference >= 60*60*24*365 )
   {
      $ago_months   = round( $difference / ( 60 * 60 * 24 * 30.5 ) ) % 12;
      $ago_months   = ( ( $ago_months AND $ago_months > 1 ) ? ' and ' . $ago_months . ' months' : ( $ago_months == 1 ? ' and '.$ago_months.' month' : '' ) );
      $ago_years   = floor( $difference / ( 60 * 60 * 24 * 365 ) );#30 * 12
      $ago_years   = $ago_years . ' year'. ($ago_years > 1 ? 's' : '' ) ;
      $time_ago   = $ago_years.$ago_months.' '.$append;
      
   }
   return $time_ago;
}

echo time_ago(time() - 1000 );


The function seems to be heavy but infact its light fast because only selected conditions are executed.
its simple to use, as in the last line of the script.
I have tested it a lot and integrated it with eAccelerator interface on my pc. its working fine and hope you will find it useful too.

Need Comments
mathiaus
My only real criticism is that you have an if and a switch that are doing exactly the same thing!

Simply moving all of the switch cases to their relevant place in the if-else-if reduces the execution time from around 4.5s to 1.5s. It also means less code so it is easier to read (along with comments!)


Code:
function time_ago( $datefrom , $dateto=-1 )
{
   if($datefrom<=0) { return "A long time ago"; }
   if($dateto==-1) { $dateto = time(); }
   
   $difference = $dateto - $datefrom;
   
   // Seconds
   if($difference < 60)
   {
      $time_ago   = $difference . ' second' . ( $difference > 1 ? 's' : '' ).' ago';
   }
   
   // Minutes
   else if( $difference < 60*60 )
   {
         $ago_seconds   = $difference % 60;
        $ago_seconds   = ( ( $ago_seconds AND $ago_seconds > 1 ) ? ' and '.$ago_seconds.' seconds' : ( $ago_seconds == 1 ? ' and '.$ago_seconds.' second' : '' ) );
        $ago_minutes   = floor( $difference / 60 );
        $ago_minutes   = $ago_minutes . ' minute' . ( $ago_minutes > 1 ? 's' : '' ).' ago';
        $time_ago      = $ago_minutes.$ago_seconds.' ago';
   }
   
   // Hours
   else if ( $difference < 60*60*24 )
   {
         $ago_minutes   = round( $difference / 60 ) % 60 ;
       $ago_minutes   = ( ( $ago_minutes AND $ago_minutes > 1 ) ? ' and ' . $ago_minutes . ' minutes' : ( $ago_minutes == 1 ? ' and ' . $ago_minutes .' minute' : '' ));
       $ago_hours      = floor( $difference / ( 60 * 60 ) );
       $ago_hours      = $ago_hours . ' hour'. ( $ago_hours > 1 ? 's' : '' );
       $time_ago      = $ago_hours.$ago_minutes.' ago';
   }
   
   // Days
   else if ( $difference < 60*60*24*7 )
   {
      $ago_hours      = round( $difference / 3600 ) % 24 ;
      $ago_hours      = ( ( $ago_hours AND $ago_hours > 1 ) ? ' and ' . $ago_hours . ' hours' : ( $ago_hours == 1 ? ' and ' . $ago_hours . ' hour' : '' ));
      $ago_days      = floor( $difference / ( 3600 * 24 ) );
      $ago_days      = $ago_days . ' day' . ($ago_days > 1 ? 's' : '' );
      $time_ago      = $ago_days.$ago_hours.' ago';
   }
   
   // Weeks
   else if ( $difference < 60*60*24*30 )
   {
      $ago_days      = round( $difference / ( 3600 * 24 ) ) % 7;
      $ago_days      = ( ( $ago_days AND $ago_days > 1 ) ? ' and '.$ago_days.' days' : ( $ago_days == 1 ? ' and '.$ago_days.' day' : '' ));
      $ago_weeks      = floor( $difference / ( 3600 * 24 * 7) );
      $ago_weeks      = $ago_weeks . ' week'. ($ago_weeks > 1 ? 's' : '' );
      $time_ago      = $ago_weeks.$ago_days.' ago';
   }
   
   // Months
   else if ( $difference < 60*60*24*365 )
   {
      $days_diff   = round( $difference / ( 60 * 60 * 24 ) );
      $ago_days   = $days_diff %  30 ;
      $ago_weeks   = round( $ago_days / 7 ) ;
      $ago_weeks   = ( ( $ago_weeks AND $ago_weeks > 1 ) ? ' and '.$ago_weeks.' weeks' : ( $ago_weeks == 1 ? ' and '.$ago_weeks.' week' : '' ) );
      $ago_months   = floor( $days_diff / 30 );
      $ago_months   = $ago_months .' month'. ( $ago_months > 1 ? 's' : '' );
      $time_ago   = $ago_months.$ago_weeks.' ago';
   }
   
   // Years
   else if ( $difference >= 60*60*24*365 )
   {
      $ago_months   = round( $difference / ( 60 * 60 * 24 * 30.5 ) ) % 12;
      $ago_months   = ( ( $ago_months AND $ago_months > 1 ) ? ' and ' . $ago_months . ' months' : ( $ago_months == 1 ? ' and '.$ago_months.' month' : '' ) );
      $ago_years   = floor( $difference / ( 60 * 60 * 24 * 365 ) );#30 * 12
      $ago_years   = $ago_years . ' year'. ($ago_years > 1 ? 's' : '' ) ;
      $time_ago   = $ago_years.$ago_months.' ago';
   }
   
   return $time_ago;
}
imagefree
mathiaus wrote:
My only real criticism is that you have an if and a switch that are doing exactly the same thing!

Simply moving all of the switch cases to their relevant place in the if-else-if reduces the execution time from around 4.5s to 1.5s. It also means less code so it is easier to read (along with comments!)


Code:
function time_ago( $datefrom , $dateto=-1 )
{
   if($datefrom<=0) { return "A long time ago"; }
   if($dateto==-1) { $dateto = time(); }
   
   $difference = $dateto - $datefrom;
   
   // Seconds
   if($difference < 60)
   {
      $time_ago   = $difference . ' second' . ( $difference > 1 ? 's' : '' ).' ago';
   }
   
   // Minutes
   else if( $difference < 60*60 )
   {
         $ago_seconds   = $difference % 60;
        $ago_seconds   = ( ( $ago_seconds AND $ago_seconds > 1 ) ? ' and '.$ago_seconds.' seconds' : ( $ago_seconds == 1 ? ' and '.$ago_seconds.' second' : '' ) );
        $ago_minutes   = floor( $difference / 60 );
        $ago_minutes   = $ago_minutes . ' minute' . ( $ago_minutes > 1 ? 's' : '' ).' ago';
        $time_ago      = $ago_minutes.$ago_seconds.' ago';
   }
   
   // Hours
   else if ( $difference < 60*60*24 )
   {
         $ago_minutes   = round( $difference / 60 ) % 60 ;
       $ago_minutes   = ( ( $ago_minutes AND $ago_minutes > 1 ) ? ' and ' . $ago_minutes . ' minutes' : ( $ago_minutes == 1 ? ' and ' . $ago_minutes .' minute' : '' ));
       $ago_hours      = floor( $difference / ( 60 * 60 ) );
       $ago_hours      = $ago_hours . ' hour'. ( $ago_hours > 1 ? 's' : '' );
       $time_ago      = $ago_hours.$ago_minutes.' ago';
   }
   
   // Days
   else if ( $difference < 60*60*24*7 )
   {
      $ago_hours      = round( $difference / 3600 ) % 24 ;
      $ago_hours      = ( ( $ago_hours AND $ago_hours > 1 ) ? ' and ' . $ago_hours . ' hours' : ( $ago_hours == 1 ? ' and ' . $ago_hours . ' hour' : '' ));
      $ago_days      = floor( $difference / ( 3600 * 24 ) );
      $ago_days      = $ago_days . ' day' . ($ago_days > 1 ? 's' : '' );
      $time_ago      = $ago_days.$ago_hours.' ago';
   }
   
   // Weeks
   else if ( $difference < 60*60*24*30 )
   {
      $ago_days      = round( $difference / ( 3600 * 24 ) ) % 7;
      $ago_days      = ( ( $ago_days AND $ago_days > 1 ) ? ' and '.$ago_days.' days' : ( $ago_days == 1 ? ' and '.$ago_days.' day' : '' ));
      $ago_weeks      = floor( $difference / ( 3600 * 24 * 7) );
      $ago_weeks      = $ago_weeks . ' week'. ($ago_weeks > 1 ? 's' : '' );
      $time_ago      = $ago_weeks.$ago_days.' ago';
   }
   
   // Months
   else if ( $difference < 60*60*24*365 )
   {
      $days_diff   = round( $difference / ( 60 * 60 * 24 ) );
      $ago_days   = $days_diff %  30 ;
      $ago_weeks   = round( $ago_days / 7 ) ;
      $ago_weeks   = ( ( $ago_weeks AND $ago_weeks > 1 ) ? ' and '.$ago_weeks.' weeks' : ( $ago_weeks == 1 ? ' and '.$ago_weeks.' week' : '' ) );
      $ago_months   = floor( $days_diff / 30 );
      $ago_months   = $ago_months .' month'. ( $ago_months > 1 ? 's' : '' );
      $time_ago   = $ago_months.$ago_weeks.' ago';
   }
   
   // Years
   else if ( $difference >= 60*60*24*365 )
   {
      $ago_months   = round( $difference / ( 60 * 60 * 24 * 30.5 ) ) % 12;
      $ago_months   = ( ( $ago_months AND $ago_months > 1 ) ? ' and ' . $ago_months . ' months' : ( $ago_months == 1 ? ' and '.$ago_months.' month' : '' ) );
      $ago_years   = floor( $difference / ( 60 * 60 * 24 * 365 ) );#30 * 12
      $ago_years   = $ago_years . ' year'. ($ago_years > 1 ? 's' : '' ) ;
      $time_ago   = $ago_years.$ago_months.' ago';
   }
   
   return $time_ago;
}


thanks mathiaus, i have updated the function and added a little validation too.
I cant understand one thing, how do you measure this performance difference (ie 4.5s to 1.5s)?
mathiaus
http://www.developerfusion.com/code/2058/determine-execution-time-in-php/
Hogwarts
I have a larger criticism, but that's questioning the "ago" orientation. I'd prefer it to be something more along the lines of time_words, that way you could have

Code:
<?php echo 'The event occurred '. time_words(1234, 4834) .' before midnight'); ?>

assuming 4834 to mean midnight. That would output "The event occurred one hour before midnight". Much, much more useful. That way you can have "before", "ago", whatever! Leave it malleable for the developer to decide Smile
imagefree
Hogwarts wrote:
I have a larger criticism, but that's questioning the "ago" orientation. I'd prefer it to be something more along the lines of time_words, that way you could have

Code:
<?php echo 'The event occurred '. time_words(1234, 4834) .' before midnight'); ?>

assuming 4834 to mean midnight. That would output "The event occurred one hour before midnight". Much, much more useful. That way you can have "before", "ago", whatever! Leave it malleable for the developer to decide Smile


thanks hogwarts! script is updated.
imagefree
mathiaus wrote:
http://www.developerfusion.com/code/2058/determine-execution-time-in-php/


using this is not only inaccurate, but also that you can still not calculate the actual time of small processes like 'if' and 'switch';

The time variation is often more than the actual time consumed. I am using the same way to calculate page load time but i am sure that this way to calculate time of a single process is not that good.
mathiaus
imagefree wrote:
mathiaus wrote:
http://www.developerfusion.com/code/2058/determine-execution-time-in-php/


using this is not only inaccurate, but also that you can still not calculate the actual time of small processes like 'if' and 'switch';

The time variation is often more than the actual time consumed. I am using the same way to calculate page load time but i am sure that this way to calculate time of a single process is not that good.


It's obviously far from perfect but still good enough to base assumptions on and to use for a rough base.
Related topics
Script php about gallery
File upload with PHP, Build an upload database for your site
[tutor] How to protect images without htaccess using PHP
Make search engine With PHP and mySQL, for your site
I want my own website desgin not PHP just my own thing 150f$
[php] Overwrite pre-made functions?
PHP Script- How can we install it
je veux integrer un script php dans mon themes.
Give me a special countdown script for 10frih$
Image Manipulation with PHP Script
CAN ANYONE SCRIPT PHP
Question about a php and mysql script.
Using free php script
[help] [tecg] script php para publicar en twitter
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.