Wordpress, How Many Days Old is the Post
December 14th, 2005 by Michael Gray in ProgrammingIf 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










December 15th, 2005 at 2:49 am
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)
December 15th, 2005 at 8:16 pm
I tried that wasn’t working on my dev server for some reason, but it works on the live one, go figure.
December 16th, 2005 at 4:03 am
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.
December 16th, 2005 at 7:28 am
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?
December 16th, 2005 at 8:11 am
*hits head on desk*
June 22nd, 2007 at 8:46 am
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.
September 20th, 2007 at 10:29 pm
Wow. You gys must have been pioneers or something. Wordpress is so easy to use these days.