Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a WordPress blog where I need to display an image on each post only for 3 weeks. This means that after 3 weeks have passed, since each post was published, the image will disappear.

First, I made sure that the image appears each time a new post is published. This was easy. Then I needed to get the date when each posts was published and I used the_time(); function.

The problem I'm having right now is that I don't know how to use the_time(); function to do anything (compare it with current time, adding 3 weeks etc).

I tried to do this with javascript using this code which works, it outputs the current date and the date after 3 weeks.

now = new Date();
threeweeksfromnow = new Date(now.getTime() + (21 * 24 * 60 * 60 * 1000));
months = new Array("January ","February ","March ","April ","May ","June ","July ","August ","September ","October ","November ","December ");

document.write("Today's date is: " + months[now.getMonth()] + now.getDate() + "<br>");

document.write("Three weeks from today is: " + months[threeweeksfromnow.getMonth()] + threeweeksfromnow.getDate() + "<br>"); 

I tried to tie this code to the_time(); function in wordpress but it didn't worked. Does anyone have any idea how I might solve this problem?

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

You should use get_the_time() and not the_time(). First one returns time and second one echoes it.

I don't think you should do this in JS though (there is no need to, and users can always block JS and see this image anyway).

Your code could look something like this (it's in PHP):

if ( strtotime( get_the_time('Y-m-d H:i:s') ) + 21*24*60*60 > time() ) {  // if post_time + 3 weeks is greater than current time
  // show your image ( the_post_thumbnail() or antyhing else you want to do)
}
share|improve this answer
    
Thanks for helping! –  George Grigorita Jun 20 '13 at 16:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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