Make WordPress Core

Changeset 45573


Ignore:
Timestamp:
06/28/2019 06:43:27 AM (6 years ago)
Author:
pento
Message:

Formatting: Add support for seconds to human_time_diff().

The web has gotten so much faster since human_time_diff() was created, we need to be able to measure time differences with much finer granularity. Now, we can.

Props johnjamesjacoby, pento.
Fixes #35655.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/formatting.php

    r45569 r45573  
    36093609 *
    36103610 * @since 1.5.0
     3611 * @since 5.3.0 Added support for showing a difference in seconds.
    36113612 *
    36123613 * @param int $from Unix timestamp from which the difference begins.
     
    36213622    $diff = (int) abs( $to - $from );
    36223623
    3623     if ( $diff < HOUR_IN_SECONDS ) {
     3624    if ( $diff < MINUTE_IN_SECONDS ) {
     3625        $secs = $diff;
     3626        if ( $secs <= 1 ) {
     3627            $secs = 1;
     3628        }
     3629        /* translators: Time difference between two dates, in seconds. %s: Number of seconds */
     3630        $since = sprintf( _n( '%s second', '%s seconds', $secs ), $secs );
     3631    } elseif ( $diff < HOUR_IN_SECONDS && $diff >= MINUTE_IN_SECONDS ) {
    36243632        $mins = round( $diff / MINUTE_IN_SECONDS );
    36253633        if ( $mins <= 1 ) {
  • trunk/tests/phpunit/tests/formatting/HumanTimeDiff.php

    r41018 r45573  
    2020    function data_test_human_time_diff() {
    2121        return array(
     22            array(
     23                '37 seconds',
     24                new DateTime( '2016-01-01 12:00:37' ),
     25                'Test a difference of 37 seconds.',
     26            ),
    2227            array(
    2328                '5 mins',
Note: See TracChangeset for help on using the changeset viewer.