Date comparison in a widget

We have a card style widget that I’m struggling with a date display. In this example: Events | Home & Garden Information Center - you can see a few events in the feed with dates in the past, and the date they are displaying are their begin date. The Glyphosate event, for example, has continuous dates from Jul 28, 2025 - Jan 11, 2028.

What we’d like to see is if this is a continuing date just to display today’s date, so I am trying to get my mind around what to do. I’m trying something like below

<xphp content="true">
  <if var="date" before="today" />
    <content>
      <div class="lwc-event-card-date repeat" aria-label="{date}">
        <div class="cal_date">
	  <div class="cal_date_month"><xphp var="server_date_month" substring="0,3" /></div>
	  <div class="cal_date_day"><xphp var="server_date_date" /></div>
        </div>
      </div>
    </content>
  <else content="true">
    <content>
      <div class="lwc-event-card-date" aria-label="{date}">{cal_date}</div>
    </content>
  </else>
</xphp>

Basically, I need:

if( start date < today ) {
  display today's date.
} else {
  display the regular date info from the event.
}

Hey Jon – this has come up a couple times in recent months (in various forms) and it’s definitely something that’s on our list to make easier LW 3.0 later this year. In the meantime, I wonder if there might be a custom module approach using onWidgetFormat that could overwrite the date variable conditionally? XPHP is also an option as you’ve started to tinker with, but I don’t know offhand if there’s straightforward syntax that would process the dates the way you’re needing. I’ll check with our team next week to see if they have any ideas, or if anyone else has suggestions please feel free!

Thanks for the quick update Karl. I appreciate you. I’ll look into that.

And BTW, we launched LiveWhale across clemson.edu last week and have loved every minute of it! We love the snappy response and excellent tooling yall provide. Thanks for everything to you and the team!

1 Like

Holy moly, Karl, that worked! I got it figured out. Compared datetime and today, and if so, replaced cal_date with the right stuff. Dig! Thanks bud!

1 Like

Glad that worked!

One tip from Alex that I completely missed earlier:

The XPHP example you gave earlier was almost working, but <xphp> inside of a widget format usually won’t work properly: XPHP variables are page-wide or system-wide variables, whereas when you’re making comparisons or showing variables inside a widget variable, you need to swap <xphp for <field.

Something like this might’ve worked as a starting point too:

<field content="true">
  <if var="date" before="today" />
    <content>
      before
    </content>
  <else content="true">
    <content>
      after
    </content>
  </else>
</field>

That is, since {date} is a widget-result-specific item, we need to use <field> to access that variable rather than <XPHP>. Hope this helps!