Skip to navigation | Skip to content



Compare the WordPress post date

I find PHP’s date handling a bit confusing. It’s easy when you know how, of course; but even at the same time as appreciating that it’s good in the end that it offers a lot of flexibility, it’s sometimes seemed like there are too many hoops to jump through to do some simple date operations.

Anyway, I’ve just cracked a minor issue I’m working on thanks to this post and this thread. I’m posting the solution here for my quick reference as much as anything…

Problem: how to check if a WordPress post_date (from a $post object) is from a day in the future? Solution:

$postDate = strtotime( $post->post_date );
$todaysDate = strtotime( date( 'Y-m-d' ) );
if ( $postDate > $todaysDate ) {
	[ post is from the future! ]
} else {
	[ post is from today or the past]
}

5 comments

  1. Oren Yomtov (21st March 2010)

    You can replace the line
    $todaysDate = strtotime( date( 'Y-m-d' ) );
    with
    $todaysDate = time();

    Also, I’ve written a post about time and date handling in PHP.

  2. Steve Taylor (21st March 2010)

    Thanks Oren – that’s a useful post.

  3. Jim (18th November 2010)

    Thanks to both of you gentlemen! Oren your solution is spot on.

  4. Winfried Hoffmann (20th March 2011)

    Thank you, your solution comes in very handy.

  5. Dexx (16th April 2011)

    Thank you so much! I was trying to find the code for this and stumbled across your page in a Google search. Cheers :)

Comments on this post are closed.

Recent posts

View complete archives »