3

I am working on a plugin and want to change the default post date to something like 'posted one min ago' OR 'posted 1 hour ago', OR 'posted on month ago'.

2 Answers 2

2

Straight from my framework as human_time_diff() involves caching.

/**
 * Time since an entry was posted
 * 
 * Adapted from binary bonsai
 * @link    http://binarybonsai.com/code/timesince.txt 
 * @license unknown - original source unavailable
 * 
 * @todo check human_time_diff(); as a replacement 
 * @link http://codex.wordpress.org/Function_Reference/human_time_diff
 * Note: Rarst said there's caching involved with this fn.
 * 
 * @param   (integer) $older_date
 * @param   (integer) $newer_date
 */
function get_time_since( $older_date, $newer_date = false )
{
    // array of time period chunks
    $chunks = array(
         array( 60 * 60 * 24 * 365 , 'year' )
        ,array( 60 * 60 * 24 * 30 , 'month' )
        ,array( 60 * 60 * 24 * 7, 'week' )
        ,array( 60 * 60 * 24 , 'day' )
        ,array( 60 * 60 , 'hour' )
        ,array( 60 , 'min' )
    );

    // $newer_date will equal false if we want to know the time elapsed between a date and the current time
    // $newer_date will have a value if we want to work out time elapsed between two known dates
    if ( $newer_date == false )
    {
        $newer_date = time() + ( 60 * 60 * get_option( 'gmt_offset' ) );
    }
    else 
    {
        $newer_date = $newer_date;
    }

    // difference in seconds
    $since = $newer_date - $older_date;

    // we only want to output two chunks of time here, eg:
    // x years, xx months
    // x days, xx hours
    // so there's only two bits of calculation below:

    // step one: the first chunk
    for ( $i = 0, $j = count( $chunks ); $i < $j; $i++ )
    {
        $seconds = $chunks[$i][0];
        $name = $chunks[$i][1];

        // finding the biggest chunk (if the chunk fits, break)
        if ( ( $count = floor( $since / $seconds ) ) != 0 )
        {
            break;
        }
    }

    // set output var
    if ( $count == true )
    { 
        $output = "1 {$name2}";
    }
    else 
    {
        $output = "{$count} {$name}s";
    }

    // step two: the second chunk
    if ( $i + 1 < $j )
    {
        $seconds2 = $chunks[$i + 1][0];
        $name2 = $chunks[$i + 1][1];

        if ( ( $count2 = floor( ( $since - ( $seconds * $count ) ) / $seconds2 ) ) != 0 )
        {
            // add to output var
            if ( $count2 == true )
            {
                $output .= ", 1 {$name2}";
            }
            else 
            {
                $output .= ", {$count} {$name2}s";
            }
        }
    }

    return $output;
}
2
  • Tried your code also and its giving me the output like this '1 , 1 month' I have used it like this <?php echo get_time_since($rst->post_date,false); ?>
    – Ravi Soni
    Nov 28, 2011 at 3:30
  • Could you post your post_date? Just echo it and set it as comment please. Ty.
    – kaiser
    Nov 28, 2011 at 4:40
4

Try this code inside your loop.

<?php echo 'posted' . human_time_diff( get_the_time('U'), current_time( 'timestamp' ) ) . ' ago'; ?>
2
  • hi joshua i have used your code but it showing weird results like '15306 days ago'
    – Ravi Soni
    Nov 28, 2011 at 3:13
  • Yup that is because it doesn't handle weeks, months and years. For that Kaiser's answer does the trick. You can try that. Nov 28, 2011 at 3:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.