| 3933 | /** |
| 3934 | * Returns a human-readable time difference between now and a given timestamp. |
| 3935 | * |
| 3936 | * An alternative to human_time_diff(). This function calculates the time |
| 3937 | * difference between two given timestamps, and returns the result |
| 3938 | * in a human-friendly string, such as: |
| 3939 | * |
| 3940 | * 4 days, 3 hours, 20 minutes, 15 seconds ago |
| 3941 | * |
| 3942 | * The timestamps are expected to be a Unix timestamp or a mysql date. |
| 3943 | * |
| 3944 | * @param int $from Timestamp to compare to $to. |
| 3945 | * @param int $to Timestamp to compare $from against. Defaults to time(). |
| 3946 | * @param int $limit Maximum number of parts (precision) in the given result. |
| 3947 | * @return string Human-readable time difference. |
| 3948 | */ |
| 3949 | function wp_natural_time( $from, $to = null, $limit = 1 ) { |
| 3950 | if ( is_null( $to ) ) { |
| 3951 | $to = time(); |
| 3952 | } |
| 3953 | if ( ! is_numeric( $from ) ) { |
| 3954 | $from = mysql2date( 'U', $from ); |
| 3955 | } |
| 3956 | if ( ! is_numeric( $to ) ) { |
| 3957 | $to = mysql2date( 'U', $to ); |
| 3958 | } |
| 3959 | $diff = absint( $to - $from ); |
| 3960 | |
| 3961 | if ( $diff < 1 ) { |
| 3962 | return apply_filters( 'wp_natural_time', _x( 'now', 'time ago' ), $from, $limit ); |
| 3963 | } |
| 3964 | |
| 3965 | $result = array(); |
| 3966 | |
| 3967 | $parts = array( |
| 3968 | /* translators: Time difference in years. %s: Number of years. */ |
| 3969 | array( YEAR_IN_SECONDS, _nx_noop( '%s year', '%s years', 'time ago' ) ), |
| 3970 | /* translators: Time difference in months. %s: Number of months. */ |
| 3971 | array( 30 * DAY_IN_SECONDS, _nx_noop( '%s month', '%s months', 'time ago' ) ), |
| 3972 | /* translators: Time difference in weeks. %s: Number of weeks. */ |
| 3973 | array( WEEK_IN_SECONDS, _nx_noop( '%s week', '%s weeks', 'time ago' ) ), |
| 3974 | /* translators: Time difference in days. %s: Number of days. */ |
| 3975 | array( DAY_IN_SECONDS, _nx_noop( '%s day', '%s days', 'time ago' ) ), |
| 3976 | /* translators: Time difference in hours. %s: Number of hours. */ |
| 3977 | array( HOUR_IN_SECONDS, _nx_noop( '%s hour', '%s hours', 'time ago' ) ), |
| 3978 | /* translators: Time difference in minutes. %s: Number of minutes. */ |
| 3979 | array( MINUTE_IN_SECONDS, _nx_noop( '%s minute', '%s minutes', 'time ago' ) ), |
| 3980 | /* translators: Time difference in seconds. %s: Number of seconds. */ |
| 3981 | array( 1, _nx_noop( '%s second', '%s seconds', 'time ago' ) ), |
| 3982 | ); |
| 3983 | |
| 3984 | foreach ( $parts as $key => $pair ) { |
| 3985 | $count = (int) ( $diff / $pair[0] ); |
| 3986 | if ( $count > 0 ) { |
| 3987 | $result[] = sprintf( translate_nooped_plural( $parts[ $key ][1], $count ), $count ); |
| 3988 | $diff -= $count * $pair[0]; |
| 3989 | } elseif ( ! empty( $result ) ) { |
| 3990 | // Units shall be adjacent, we already have something |
| 3991 | // but the next unit just turned out to be 0, so stop the loop. |
| 3992 | break; |
| 3993 | } |
| 3994 | |
| 3995 | if ( $limit && count( $result ) >= $limit ) { |
| 3996 | break; |
| 3997 | } |
| 3998 | } |
| 3999 | |
| 4000 | /* translators: Time difference. %s: Time as a string. */ |
| 4001 | $label = ( $to > $from ) ? _x( '%s ago', 'time ago' ) : _x( '%s from now', 'time from now' ); |
| 4002 | $result = implode( _x( ', ', 'natural time separator' ), $result ); |
| 4003 | $result = sprintf( $label, $result ); |
| 4004 | |
| 4005 | /** |
| 4006 | * Filters the human readable time difference. |
| 4007 | * |
| 4008 | * @since 6.9.0 |
| 4009 | * |
| 4010 | * @param string $result The human readable time difference. |
| 4011 | * @param int $from Unix timestamp from which the difference begins. |
| 4012 | * @param int $to Unix timestamp to end the time difference. |
| 4013 | * @param int $limit Maximum number of time components to show. |
| 4014 | * @param int $diff The difference in seconds. |
| 4015 | */ |
| 4016 | return apply_filters( 'wp_natural_time', $result, $from, $to, $limit, $diff ); |
| 4017 | } |
| 4018 | |