Make WordPress Core

Ticket #9272: 9272.patch

File 9272.patch, 2.5 KB (added by Viper007Bond, 16 years ago)

First pass

  • wp-includes/formatting.php

     
    14621462 *
    14631463 * @param int $from Unix timestamp from which the difference begins.
    14641464 * @param int $to Optional. Unix timestamp to end the time difference. Default becomes time() if not set.
     1465 * @param int $limit Optional. The number of unit types to display (i.e. the accuracy). Defaults to 1.
    14651466 * @return string Human readable time difference.
    14661467 */
    1467 function human_time_diff( $from, $to = '' ) {
     1468function human_time_diff( $from, $to = '', $limit = 1 ) {
     1469        // Since all months/years aren't the same, these values are what Google's calculator says
     1470        $units = apply_filters( 'time_units', array(
     1471                31556926 => array( __('%s year'),  __('%s years') ),
     1472                2629744  => array( __('%s month'), __('%s months') ),
     1473                604800   => array( __('%s week'),  __('%s weeks') ),
     1474                86400    => array( __('%s day'),   __('%s days') ),
     1475                3600     => array( __('%s hour'),  __('%s hours') ),
     1476                60       => array( __('%s min'),   __('%s mins') ),
     1477        ) );
     1478
    14681479        if ( empty($to) )
    14691480                $to = time();
    1470         $diff = (int) abs($to - $from);
    1471         if ($diff <= 3600) {
    1472                 $mins = round($diff / 60);
    1473                 if ($mins <= 1) {
    1474                         $mins = 1;
    1475                 }
    1476                 $since = sprintf(_n('%s min', '%s mins', $mins), $mins);
    1477         } else if (($diff <= 86400) && ($diff > 3600)) {
    1478                 $hours = round($diff / 3600);
    1479                 if ($hours <= 1) {
    1480                         $hours = 1;
    1481                 }
    1482                 $since = sprintf(_n('%s hour', '%s hours', $hours), $hours);
    1483         } elseif ($diff >= 86400) {
    1484                 $days = round($diff / 86400);
    1485                 if ($days <= 1) {
    1486                         $days = 1;
    1487                 }
    1488                 $since = sprintf(_n('%s day', '%s days', $days), $days);
     1481
     1482        $from = (int) $from;
     1483        $to   = (int) $to;
     1484        $diff = (int) abs( $to - $from );
     1485
     1486        $items = 0;
     1487        $output = array();
     1488
     1489        foreach ( $units as $unitsec => $unitnames ) {
     1490                if ( $items >= $limit )
     1491                        break;
     1492
     1493                if ( $diff < $unitsec )
     1494                        continue;
     1495
     1496                $numthisunits = floor( $diff / $unitsec );
     1497                $diff = $diff - ( $numthisunits * $unitsec );
     1498                $items++;
     1499
     1500                if ( $numthisunits > 0 )
     1501                        $output[] = sprintf( _n( $unitnames[0], $unitnames[1], $numthisunits ), $numthisunits );
    14891502        }
    1490         return $since;
     1503
     1504        // translators: The seperator for human_time_diff() which seperates the years, months, etc.
     1505        $seperator = _x( ', ', 'human_time_diff' );
     1506
     1507        if ( !empty($output) ) {
     1508                return implode( $seperator, $output );
     1509        } else {
     1510                $smallest = array_pop( $units );
     1511                return sprintf( $smallest[0], 1 );
     1512        }
    14911513}
    14921514
    14931515/**