WordPress.org

Forums

The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

How many days ago a post is posted (5 posts)

  1. mstegink
    Member
    Posted 10 years ago #

    I am working on my new theme and for any post I would like to include the number of days ago that post has been posted, like at garrettdimon.com. I did something similar on my site but it does give me some strange numbers. The code I am using is

    $date1 = strtotime(date('d-m-y'));
    $date2 = strtotime(the_date('d-m-y', '', '', false));
    $total_days = round(($date1-$date2)/(24*3600));
    echo $total_days." days old";

    Any clue?

  2. Joshua Sigar
    Member
    Posted 10 years ago #

    Search for a plugin keyword "time since"

  3. Lester Chan
    Member
    Posted 10 years ago #

    http://dev.wp-plugins.org/wiki/wp-relativedate

    It will post x days since, if more than 7 days then it is weeks since. If the post is posted a month ago, it will not display anything.

  4. Alex Mills (Viper007Bond)
    Moderator
    Posted 10 years ago #

  5. mstegink
    Member
    Posted 10 years ago #

    I didn't want to use a plugin but rather include some code in the theme. I found this code which works out quite nicely. It only gives you the days and zero days if younger than one day. That's fine for me, nice and clean. Check it out on my site.

    $postdate = str_replace("-", " ",substr($post->post_date, 0,10));
    $postd = explode(" ", $postdate);
    $ageunix = (( time() - mktime('','','', $postd[1], $postd[2], $postd[0])));
    $days_old = floor($ageunix/(24*60*60));
    echo $days_old." days old";

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags