Published by Mijingo

tips icon image

EE Insider Tips
Sponsored by Mijingo's EE 2 Screencasts

ExpressionEngine mini-howtos created by the EE Insider community.

More precise date comparisons

  • Posted by Jason Morehead
  • June 22, 2009

As with most EE variables, you can do comparisons on date variables, like so:

{if date_variable == "foo"}
    
Do something
{
/if} 

However, if you need more precise date comparisons, you can do something like this:

{if "{date_variable format='%m/%d/%Y'}" == "01/01/2009"}
    
Do something
{
/if} 

You can also use this to compare two date variables (for example, comparing a start and end date for an event):

{if "{start_date format='%m%d%Y'}" == "{end_date format='%m%d%Y'}"}
    {start_date format
="%F %j, %Y"}
{if
:else}
    {start_date format
="%F %j"}-{end_date format="%j, %Y"}
{
/if} 

I recently used this on a project, and it came in very handy.