daily pastebin goal
70%
SHARE
TWEET

Viper007Bond

a guest Oct 21st, 2010 403 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Determines the difference between two timestamps.
  3.  *
  4.  * The difference is returned in a human readable format such as "1 hour",
  5.  * "5 mins", "2 days".
  6.  *
  7.  * @since 1.5.0
  8.  *
  9.  * @param int $from Unix timestamp from which the difference begins.
  10.  * @param int $to Optional. Unix timestamp to end the time difference. Default becomes time() if not set.
  11.  * @param int $limit Optional. The number of unit types to display (i.e. the accuracy). Defaults to 1.
  12.  * @return string Human readable time difference.
  13.  */
  14. function human_time_diff( $from, $to = '', $limit = 1 ) {
  15.         // Since all months/years aren't the same, these values are what Google's calculator says
  16.         $units = apply_filters( 'time_units', array(
  17.                         31556926 => array( __('%s year'),  __('%s years') ),
  18.                         2629744  => array( __('%s month'), __('%s months') ),
  19.                         604800   => array( __('%s week'),  __('%s weeks') ),
  20.                         86400    => array( __('%s day'),   __('%s days') ),
  21.                         3600     => array( __('%s hour'),  __('%s hours') ),
  22.                         60       => array( __('%s min'),   __('%s mins') ),
  23.         ) );
  24.  
  25.         if ( empty($to) )
  26.                 $to = time();
  27.  
  28.         $from = (int) $from;
  29.         $to   = (int) $to;
  30.         $diff = (int) abs( $to - $from );
  31.  
  32.         $items = 0;
  33.         $output = array();
  34.  
  35.         foreach ( $units as $unitsec => $unitnames ) {
  36.                         if ( $items >= $limit )
  37.                                         break;
  38.  
  39.                         if ( $diff < $unitsec )
  40.                                         continue;
  41.  
  42.                         $numthisunits = floor( $diff / $unitsec );
  43.                         $diff = $diff - ( $numthisunits * $unitsec );
  44.                         $items++;
  45.  
  46.                         if ( $numthisunits > 0 )
  47.                                         $output[] = sprintf( _n( $unitnames[0], $unitnames[1], $numthisunits ), $numthisunits );
  48.         }
  49.  
  50.  
  51.         // translators: The seperator for human_time_diff() which seperates the years, months, etc.
  52.         $seperator = _x( ', ', 'human_time_diff' );
  53.  
  54.         if ( !empty($output) ) {
  55.                         return implode( $seperator, $output );
  56.         } else {
  57.                         $smallest = array_pop( $units );
  58.                         return sprintf( $smallest[0], 1 );
  59.         }
  60. }
RAW Paste Data
Top