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]
}

2 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.

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Want to show me some code? Don't paste long code here, link to a post at pastebin.ca or a similar site. Thanks.

Recent posts

Archives