| 1844 | * When passing an array of arguments, the arguments are as follows: |
| 1845 | * |
| 1846 | * The "from" argument is the starting Unix timestamp. It is the only required argument. |
| 1847 | * |
| 1848 | * The "to" argument is the ending Unix timestamp. It defaults to the value of time(). |
| 1849 | * |
| 1850 | * The "limit" argument controls the maximum number of units to output, i.e. the accuracy. |
| 1851 | * A limit of 1 could output something like "2 years" while a limit of 3 could output |
| 1852 | * something like "2 years, 3 months, 10 days". The default value is 1. |
| 1853 | * |
| 1854 | * The "seconds" argument is a boolean argument that controls whether to output the |
| 1855 | * number of seconds, assuming that the limit value is not reached. Defaults to false. |
| 1856 | * |
| 1857 | * The "labels" argument allows the unit labels to be customized. An associative array |
| 1858 | * should be passed with the keys being any of the following: "years", "months", "weeks", |
| 1859 | * "days", "hours", "minutes", "seconds". The array values should themselves be an array, |
| 1860 | * the first value being the singular unit name and the second value being the multiple |
| 1861 | * unit name. Any missing keys will be set to the default. For example: |
| 1862 | * <code> |
| 1863 | * $args['labels'] = array( |
| 1864 | * 'minutes' => array( 'minute', 'minutes' ), |
| 1865 | * 'seconds' => array( 'second', 'seconds' ), |
| 1866 | * ); |
| 1867 | * </code> |
| 1868 | * |
| 1869 | * The "separator" argument controls the string that goes between the returned units. |
| 1870 | * It defaults to ", " (note the space). |
| 1871 | * |
| 1872 | * The "context" argument is entirely for translation purposes. Unlike in the English |
| 1873 | * language, unit names like "days" can vary depending on the context used. For details, |
| 1874 | * @see http://core.trac.wordpress.org/ticket/18495 |
| 1875 | * |
1850 | | function human_time_diff( $from, $to = '' ) { |
1851 | | if ( empty($to) ) |
1852 | | $to = time(); |
1853 | | $diff = (int) abs($to - $from); |
1854 | | if ($diff <= 3600) { |
1855 | | $mins = round($diff / 60); |
1856 | | if ($mins <= 1) { |
1857 | | $mins = 1; |
1858 | | } |
1859 | | /* translators: min=minute */ |
1860 | | $since = sprintf(_n('%s min', '%s mins', $mins), $mins); |
1861 | | } else if (($diff <= 86400) && ($diff > 3600)) { |
1862 | | $hours = round($diff / 3600); |
1863 | | if ($hours <= 1) { |
1864 | | $hours = 1; |
1865 | | } |
1866 | | $since = sprintf(_n('%s hour', '%s hours', $hours), $hours); |
1867 | | } elseif ($diff >= 86400) { |
1868 | | $days = round($diff / 86400); |
1869 | | if ($days <= 1) { |
1870 | | $days = 1; |
1871 | | } |
1872 | | $since = sprintf(_n('%s day', '%s days', $days), $days); |
| 1882 | function human_time_diff( $args ) { |
| 1883 | |
| 1884 | // Legacy argument format ( $from, $to ) |
| 1885 | if ( ! is_array( $args ) ) { |
| 1886 | $args = array(); |
| 1887 | |
| 1888 | if ( ! $arg_count = func_num_args() ) |
| 1889 | return false; |
| 1890 | |
| 1891 | $args['from'] = func_get_arg( 0 ); |
| 1892 | if ( $arg_count >= 2 ) |
| 1893 | $args['to'] = func_get_arg( 1 ); |
1874 | | return $since; |
| 1895 | |
| 1896 | // Required argument |
| 1897 | if ( ! isset( $args['from'] ) ) |
| 1898 | return false; |
| 1899 | |
| 1900 | $defaults = array( |
| 1901 | 'to' => time(), |
| 1902 | 'limit' => 1, |
| 1903 | 'seconds' => false, |
| 1904 | 'labels' => array(), |
| 1905 | // translators: The seperator for human_time_diff() which seperates the years, months, etc. |
| 1906 | 'separator' => _x( ', ', 'human_time_diff' ), |
| 1907 | 'context' => 'duration', |
| 1908 | ); |
| 1909 | |
| 1910 | $args = wp_parse_args( $args, $defaults ); |
| 1911 | |
| 1912 | switch ( $args['context'] ) { |
| 1913 | case 'age': |
| 1914 | $default_labels = array( |
| 1915 | 'years' => array( _x( '%d year', 'age' ), _x( '%d years', 'age' ) ), |
| 1916 | 'months' => array( _x( '%d month', 'age' ), _x( '%d months', 'age' ) ), |
| 1917 | 'weeks' => array( _x( '%d week', 'age' ), _x( '%d weeks', 'age' ) ), |
| 1918 | 'days' => array( _x( '%d day', 'age' ), _x( '%d days', 'age' ) ), |
| 1919 | 'hours' => array( _x( '%d hour', 'age' ), _x( '%d hours', 'age' ) ), |
| 1920 | 'minutes' => array( _x( '%d min', 'age' ), _x( '%d mins', 'age' ) ), |
| 1921 | 'seconds' => array( _x( '%d sec', 'age' ), _x( '%d secs', ' age' ) ), |
| 1922 | ); |
| 1923 | break; |
| 1924 | |
| 1925 | case 'duration': |
| 1926 | default; |
| 1927 | $default_labels = array( |
| 1928 | 'years' => array( _x( '%d year', 'duration' ), _x( '%d years', 'duration' ) ), |
| 1929 | 'months' => array( _x( '%d month', 'duration' ), _x( '%d months', 'duration' ) ), |
| 1930 | 'weeks' => array( _x( '%d week', 'duration' ), _x( '%d weeks', 'duration' ) ), |
| 1931 | 'days' => array( _x( '%d day', 'duration' ), _x( '%d days', 'duration' ) ), |
| 1932 | 'hours' => array( _x( '%d hour', 'duration' ), _x( '%d hours', 'duration' ) ), |
| 1933 | 'minutes' => array( _x( '%d min', 'duration' ), _x( '%d mins', 'duration' ) ), |
| 1934 | 'seconds' => array( _x( '%d sec', 'duration' ), _x( '%d secs', ' duration' ) ), |
| 1935 | ); |
| 1936 | } |
| 1937 | $args['labels'] = wp_parse_args( $args['labels'], $default_labels ); |
| 1938 | |
| 1939 | // Since all months/years aren't the same, these values are what Google's calculator says |
| 1940 | // Make sure to keep this sorted from largest unit to smallest unit |
| 1941 | $units = array( |
| 1942 | 'years' => 31556926, |
| 1943 | 'months' => 2629744, |
| 1944 | 'weeks' => 604800, |
| 1945 | 'days' => 86400, |
| 1946 | 'hours' => 3600, |
| 1947 | 'minutes' => 60, |
| 1948 | ); |
| 1949 | if ( $args['seconds'] ) |
| 1950 | $units['seconds'] = 1; |
| 1951 | |
| 1952 | $args['from'] = (int) $args['from']; |
| 1953 | $args['to'] = (int) $args['to']; |
| 1954 | |
| 1955 | if ( empty( $args['to'] ) ) |
| 1956 | $args['to'] = time(); |
| 1957 | |
| 1958 | $diff = abs( $args['to'] - $args['from'] ); |
| 1959 | |
| 1960 | $output = array(); |
| 1961 | |
| 1962 | foreach ( $units as $unit_type => $unit_duration ) { |
| 1963 | if ( count( $output ) >= $args['limit'] ) |
| 1964 | break; |
| 1965 | |
| 1966 | if ( $diff < $unit_duration ) |
| 1967 | continue; |
| 1968 | |
| 1969 | $number_of_this_units = floor( $diff / $unit_duration ); |
| 1970 | $diff = $diff - ( $number_of_this_units * $unit_duration ); |
| 1971 | |
| 1972 | if ( $number_of_this_units > 0 ) |
| 1973 | $output[] = sprintf( _n( $args['labels'][$unit_type][0], $args['labels'][$unit_type][1], $number_of_this_units ), $number_of_this_units ); |
| 1974 | } |
| 1975 | |
| 1976 | if ( ! empty( $output ) ) { |
| 1977 | if ( is_rtl() ) |
| 1978 | $output = array_reverse( $output ); |
| 1979 | |
| 1980 | return implode( $args['separator'], $output ); |
| 1981 | } else { |
| 1982 | // Return "1 {unit}" with the unit being the smallest unit |
| 1983 | end( $units ); |
| 1984 | $smallest = key( $units ); |
| 1985 | return sprintf( $args['labels'][$smallest][0], 1 ); |
| 1986 | } |
| 1990 | * Determines the difference between two timestamps in a human readable format in the context of an age. |
| 1991 | * |
| 1992 | * @see human_time_diff() |
| 1993 | * @see http://core.trac.wordpress.org/ticket/18495 |
| 1994 | * |
| 1995 | * @since 3.4.0 |
| 1996 | * |
| 1997 | * @param array|int $args Either an array of arguments or the Unix timestamp from which the difference begins. |
| 1998 | * @param int $to Optional. Unix timestamp to end the time difference. Default becomes time() if not set. Ignored if first argument is an array. |
| 1999 | * @return string Human readable time difference. |
| 2000 | */ |
| 2001 | function human_time_diff_age( $args ) { |
| 2002 | $args['context'] = 'age'; |
| 2003 | return human_time_diff( $args ); |
| 2004 | } |
| 2005 | |
| 2006 | /** |
| 2007 | * Determines the difference between two timestamps in a human readable format in the context of a duration. |
| 2008 | * |
| 2009 | * @see human_time_diff() |
| 2010 | * @see http://core.trac.wordpress.org/ticket/18495 |
| 2011 | * |
| 2012 | * @since 3.4.0 |
| 2013 | * |
| 2014 | * @param array|int $args Either an array of arguments or the Unix timestamp from which the difference begins. |
| 2015 | * @param int $to Optional. Unix timestamp to end the time difference. Default becomes time() if not set. Ignored if first argument is an array. |
| 2016 | * @return string Human readable time difference. |
| 2017 | */ |
| 2018 | function human_time_diff_duration( $args ) { |
| 2019 | $args['context'] = 'duration'; |
| 2020 | return human_time_diff( $args ); |
| 2021 | } |
| 2022 | |
| 2023 | /** |