| 2526 | * Returns a human-readable time difference between now and a given timestamp. |
| 2527 | * |
| 2528 | * An alternative to human_time_diff(). This function calculates the time |
| 2529 | * difference between now and a given Unix timestamp, and returns the result |
| 2530 | * in a human-friendly string, such as: |
| 2531 | * |
| 2532 | * 4 days, 3 hours, 20 minutes, 15 seconds ago |
| 2533 | * |
| 2534 | * @param int $timestamp Unix timestamp to compare to time(). |
| 2535 | * @param int $limit Maximum number of parts (precision) in the given result. |
| 2536 | * @return string Human-readable time difference. |
| 2537 | */ |
| 2538 | function wp_natural_time( $timestamp, $limit = 1 ) { |
| 2539 | $from = time(); |
| 2540 | $diff = absint( $from - $timestamp ); |
| 2541 | |
| 2542 | if ( $diff < 1 ) { |
| 2543 | return apply_filters( 'wp_natural_time', _x( 'now', 'time ago' ), $timestamp, $limit ); |
| 2544 | } |
| 2545 | |
| 2546 | $result = array(); |
| 2547 | |
| 2548 | $l10n = array( |
| 2549 | array( YEAR_IN_SECONDS, _nx_noop( '%s year', '%s years', 'time ago' ) ), |
| 2550 | array( 30 * DAY_IN_SECONDS, _nx_noop( '%s month', '%s months', 'time ago' ) ), |
| 2551 | array( WEEK_IN_SECONDS, _nx_noop( '%s week', '%s weeks', 'time ago' ) ), |
| 2552 | array( DAY_IN_SECONDS, _nx_noop( '%s day', '%s days', 'time ago' ) ), |
| 2553 | array( HOUR_IN_SECONDS, _nx_noop( '%s hour', '%s hours', 'time ago' ) ), |
| 2554 | array( MINUTE_IN_SECONDS, _nx_noop( '%s minute', '%s minutes', 'time ago' ) ), |
| 2555 | array( 1, _nx_noop( '%s second', '%s seconds', 'time ago' ) ), |
| 2556 | ); |
| 2557 | |
| 2558 | foreach ( $l10n as $key => $pair ) { |
| 2559 | $count = (int) ( $diff / $pair[0] ); |
| 2560 | if ( $count > 0 ) { |
| 2561 | $result[] = sprintf( translate_nooped_plural( $l10n[ $key ][1], $count ), $count ); |
| 2562 | $diff -= $count * $pair[0]; |
| 2563 | } |
| 2564 | |
| 2565 | if ( $limit && count( $result ) >= $limit ) { |
| 2566 | break; |
| 2567 | } |
| 2568 | } |
| 2569 | |
| 2570 | $label = ( $from > $timestamp ) ? _x( '%s ago', 'time ago' ) : _x( '%s from now', 'time from now' ); |
| 2571 | $result = implode( _x( ', ', 'natural time separator' ), $result ); |
| 2572 | $result = sprintf( $label, $result ); |
| 2573 | |
| 2574 | return apply_filters( 'wp_natural_time', $result, $timestamp, $limit ); |
| 2575 | } |
| 2576 | |
| 2577 | /** |