1467 | | function human_time_diff( $from, $to = '' ) { |
| 1468 | function 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 | |
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 ); |