diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index 334806b9e0..12e5bee017 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -3608,6 +3608,7 @@ function sanitize_email( $email ) { * "5 mins", "2 days". * * @since 1.5.0 + * @since 5.3.0 Added support for showing a difference in seconds. * * @param int $from Unix timestamp from which the difference begins. * @param int $to Optional. Unix timestamp to end the time difference. Default becomes time() if not set. @@ -3620,7 +3621,14 @@ function human_time_diff( $from, $to = '' ) { $diff = (int) abs( $to - $from ); - if ( $diff < HOUR_IN_SECONDS ) { + if ( $diff < MINUTE_IN_SECONDS ) { + $secs = $diff; + if ( $secs <= 1 ) { + $secs = 1; + } + /* translators: Time difference between two dates, in seconds. %s: Number of seconds */ + $since = sprintf( _n( '%s second', '%s seconds', $secs ), $secs ); + } elseif ( $diff < HOUR_IN_SECONDS && $diff >= MINUTE_IN_SECONDS ) { $mins = round( $diff / MINUTE_IN_SECONDS ); if ( $mins <= 1 ) { $mins = 1; diff --git a/tests/phpunit/tests/formatting/HumanTimeDiff.php b/tests/phpunit/tests/formatting/HumanTimeDiff.php index da1ad2dcfc..473f5325d3 100644 --- a/tests/phpunit/tests/formatting/HumanTimeDiff.php +++ b/tests/phpunit/tests/formatting/HumanTimeDiff.php @@ -19,6 +19,11 @@ class Tests_Formatting_HumanTimeDiff extends WP_UnitTestCase { // Data for test_human_time_diff. function data_test_human_time_diff() { return array( + array( + '37 seconds', + new DateTime( '2016-01-01 12:00:37' ), + 'Test a difference of 37 seconds.', + ), array( '5 mins', new DateTime( '2016-01-01 12:05:00' ),