Steve Taylor photo

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. Dexx avatar Dexx

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

Comments are closed.