1850 | | function human_time_diff( $from, $to = '' ) { |
1851 | | if ( empty($to) ) |
| 1852 | function human_time_diff( $from, $to = null, $limit = 1, $seconds = false ) { |
| 1853 | // Since all months/years aren't the same, these values are what Google's calculator says |
| 1854 | // This needs to be sorted from largest unit to smallest unit |
| 1855 | $units = array( |
| 1856 | 31556926 => array( __( '%d year' ), __( '%d years' ) ), |
| 1857 | 2629744 => array( __( '%d month' ), __( '%d months' ) ), |
| 1858 | 604800 => array( __( '%d week' ), __( '%d weeks' ) ), |
| 1859 | 86400 => array( __( '%d day' ), __( '%d days' ) ), |
| 1860 | 3600 => array( __( '%d hour' ), __( '%d hours' ) ), |
| 1861 | 60 => array( __( '%d min' ), __( '%d mins' ) ), |
| 1862 | ); |
| 1863 | |
| 1864 | if ( $seconds ) |
| 1865 | $units[1] = array( __( '%d sec' ), __( '%d secs' ) ); |
| 1866 | |
| 1867 | $units = apply_filters( 'time_units', $units ); |
| 1868 | |
| 1869 | $from = (int) $from; |
| 1870 | $to = (int) $to; |
| 1871 | |
| 1872 | if ( empty( $to ) ) |
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); |
| 1874 | |
| 1875 | $diff = abs( $to - $from ); |
| 1876 | |
| 1877 | $items = 0; |
| 1878 | $output = array(); |
| 1879 | |
| 1880 | foreach ( $units as $unitsec => $unitnames ) { |
| 1881 | // Stop when the item limit is reached |
| 1882 | if ( $items >= $limit ) |
| 1883 | break; |
| 1884 | |
| 1885 | // Make sure the unit size is smaller than the remainder seconds |
| 1886 | if ( $diff < $unitsec ) |
| 1887 | continue; |
| 1888 | |
| 1889 | // Find the whole number of this unit |
| 1890 | $numthisunits = floor( $diff / $unitsec ); |
| 1891 | $diff = $diff - ( $numthisunits * $unitsec ); |
| 1892 | $items++; |
| 1893 | |
| 1894 | if ( $numthisunits > 0 ) |
| 1895 | $output[] = sprintf( _n( $unitnames[0], $unitnames[1], $numthisunits ), $numthisunits ); |
1874 | | return $since; |
| 1897 | |
| 1898 | // translators: The seperator for human_time_diff() which seperates the years, months, etc. |
| 1899 | $seperator = _x( ', ', 'human_time_diff' ); |
| 1900 | |
| 1901 | if ( ! empty( $output ) ) { |
| 1902 | if ( is_rtl() ) |
| 1903 | $output = array_reverse( $output ); |
| 1904 | |
| 1905 | return implode( $seperator, $output ); |
| 1906 | } else { |
| 1907 | // Return "1 {unit}" with the unit being the smallest unit |
| 1908 | $smallest = array_pop( $units ); |
| 1909 | return sprintf( $smallest[0], 1 ); |
| 1910 | } |