WordPress: Display content based on time

Apr 30, 2011

Post Date

Here is a quick way to display content if a post was created within a certain timeframe. For example, if you wanted to mark a post as “new”, this is one way you could accomplish this.

First we need to check the timestamp by using strtotime. This will compare the date of the post to today’s date. Then we set a range, in this example we are checking to see if the post was created in last 30 days and if it was we display the content.

<?php if (strtotime($post->post_date) > strtotime('-30 days')) {
    // Posted in the last 30 days

Member Registration Date

Maybe you want to display information based on a user’s registration date. Here is a quick example of how using strtotime we can accomplish this.

In the example below we are simply creating a function to retrive information for the current user and then comparing the user_registered date to the current date. We are also determining if the user has registered in the last 30 days, and if so then we display the content.

<?php get_currentuserinfo(); 
$user_data = get_userdata($user_ID);
$registered_date = $user_data->user_registered;
if (strtotime($registered_date) > strtotime('-30 days')) {
   //User registered in the last 30 days