May
20th

WordPress: Was this post written today?

Thanks for stopping by my personal blog on Marketing Technology! Over 50,000 visitors a month find my content worth returning for, so don't forget to subscribe to the Marketing Technology Blog RSS feed or to the Marketing Technology Email to have new content sent directly to your inbox. You may also find my other business blog helpful, Social Media Domination.

CalendarI’ve been wanting to add the little calendar graphics to my posts for a while now. If you’ve not seen them, they’re on the web version… click on over and take a look! I started with some graphics from another site, but decided that I wanted to spice it up a little bit. So, I wrote two classes for the date div and set the background image differently based on whether or not the post was written today.

Thanks to Michael H in the WordPress Support Forums, I finally got my statement correct! Here’s what I did. I have a background image set for the div class date:

<div class=”the_date”>

For today’s div, I set a different background image applied to a div class named the_date_today:

<div class=”the_date_today”>

Now that I’ve got those set, I need to write some code that adds the “_today” if the post was written today:

<div class=”the_date<?php $post_date = mysql2date(’Ymd’, $post->post_date_gmt); if($post_date==gmdate(’Ymd’)) { echo ‘_today’; } ?>”>

Here’s how this works:

  1. I set a variable called $post_date equal to the date of the post formatted as Ymd.
  2. I write an if statement so that if that variable equals today’s date (formatted as Ymd as well), I add “_today”

Voila! Now I have a calendar graphic that displays whether or not the post was written today! I just need to adjust for timezone and I’ll have it made!

RSS feed | Trackback URI

5 Comments »

Comment by no imageSean (SezWho)
2007-05-20 20:06:47

Hey Doug. That’s really slick!

Side note, I suggest you move your ’subscribe to’ check box above the add comment button… to me that’s a little more user friendly.

Great job on your new calendar graphics and CSS.

Rate this:
2.9
Comment by no imageDouglas Karr (SezWho)
2007-05-20 21:10:23

Thanks Sean.

The positioning of the check box is on purpose. Putting it outside the other fields would create separation between it and the other tightly spaced fields. By placing it near the button, it’s putting a selection near an action, this actually might cause more people to miss it as they complete their thoughts in a comment and move to submit.

One thing that is missing is proper tab stops, though. I am going to correct that.

Rate this:
2.9
 
 
Comment by no imageSean (SezWho)
2007-05-21 13:50:00

Well I think there is a bug in your code now that it’s a new day. The calendar icon still says today but it’s actually tomorrow now :)
Rate this:
2.9
Comment by no imageDouglas Karr (SezWho)
2007-05-21 14:00:01

The last sentence of the the post states the issue - I have to adjust for GMT. I also need to adjust for Caching so I’m trying to kill 2 birds with 1 stone.
Rate this:
2.9
 
 
Comment by no imageSean (SezWho)
2007-05-21 14:35:28

Ok, I didn’t realize that’s what you were meaning about adjusting for GMT.

I’m sure you’re on top of it mr code monkey :) but maybe you could do some sort of ‘if’ statement looking at your server time?

if server date/time is X compared to post date/time show X image or something to that effect.

Rate this:
2.9
 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.

My Comment Policy: I moderate comments. Please be patient:

  • Spam will happily be destroyed.
  • Use your real name, not some keywords. Otherwise it will be destroyed.
  • Mean comments aren't necessary. If I don't post them I will reply personally to let you know why.
  • Lewd comments will be edited, I don't want my readers leaving because of offensive content.
Great debate, criticism and colorful commentary is always appreciated and approved!