Wordpress, How Many Days Old is the Post

December 14th, 2005 by Michael Gray in Programming


If you're new here, you may want to subscribe to my RSS feed. Read my top posts or learn more about Michael Gray. Want more frequent updates follow me on Twitter. Thanks for visiting!

Over on Performancing.com Andy, says the money is in the archives, stupid. Nick talks about Monetizing your Blog, by showing different levels of advertising based on how old post is and whether someone is logged in or not. Now Andy’s doing a blog monetization makeover, and time sensitive ads came up again. Since I’ve got the hood open on this WordPress install I figured I would work up some code to figure out how old a post is.


$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));

Basically what I’m doing is getting time and date of the post. I then isolate the just date and remove out “-” characters. I explode it into an array, convert them to unixtime, and do the subtraction. Then convert it back from UNIX time to days old expressed as an integer value (my high school math teacher Sister Miriam would be proud).

Sphere It

Text Link Ads


7 Responses to “Wordpress, How Many Days Old is the Post”

  1. Ozh Says:

    Spare yourself a few lines of code :


    $ageunix = time() - get_the_time(’U');
    $days_old = floor($ageunix/(24*60*60));

    (on a side note, your captcha system is way too annoying… Had to try 3 times to get this comment posted. There are many WP plugins less intrusive and as effective)

  2. Administrator Says:

    I tried that wasn’t working on my dev server for some reason, but it works on the live one, go figure.

  3. Cal Says:

    Well you can otherwise spare a line by not replacing the dashes with spaces. Just perform the explode on the dash.

    Does wordpress not have an accountsystem? I have to add my name and email etc again everytime I post.

  4. Administrator Says:

    Does wordpress not have an accountsystem? I have to add my name and email etc again everytime I post.

    Sure it has an accountant system, didn’t you get your password? ;-)

  5. Cal Says:

    *hits head on desk*

  6. Squid Says:

    I’m glad wordpress has sorted all this out now. It’s like you had to be a genius to work this out. Lucky for us now.

  7. Nancy S Says:

    Wow. You gys must have been pioneers or something. Wordpress is so easy to use these days.