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

  1. mstegink
    Posted 9 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
    Posted 9 years ago #

    Search for a plugin keyword "time since"

  3. Lester Chan
    Posted 9 years ago #


    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. mstegink
    Posted 9 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