Get specific parts of full date?

Working on a new widget, and it displays date information in a couple of ways. The header is set to show the month and year, ex: October 2026. There’s a section on the left with something that looks like how cal_date renders, but is just the day of the week and the date, ex: THUR 23. Then in the description area of the event, it displays a more complete date/time, ex: October 23 @ 5:30 - 7:30 p.m.

The header and description times are easy enough to do. For the other one, I’m a little stumped. Easiest thing to me would be to modify what cal_date outputs. Otherwise, I’d expect to use some more specific variables, something like date_weekday maybe. Is there a way to accomplish either of these options?

@omcnamar Something like this should work:

<div>
    <div><field var="date" date_format="D" /></div>
    <div><field var="date" date_format="j" /></div>
</div>
<div>{title}</div>
<div>{date}, {time}</div>

You can use the date in a <field> with a date_format attribute. You can use it multiple times with different formats to get different parts of the date. Then style with CSS.

1 Like

Hi there – sure thing, using <field var you can re-cast a date using date_format= and any PHP-valid date string, e.g. <field var="date" date_format="M"/> and <field var="date" date_format="j"/>. Hope this helps!

Edit: Once again @jwilcox beat me to it! :wink: :dancing_women:Jinx.

Barely. Your comment appeared seconds after mine.

Very cool trick, thank you both! Is that in the docs somewhere? I’m wondering what other secrets I might have missed.

I don’t think it’s in the docs. Not sure how I figured it out years ago, but it is a good trick to know.

Good call! I think it snuck by the xphp and widget formatting docs where I’d expect to see it – we’ll get that added :+1:

:white_check_mark: Added to the docs in both spots. :+1:

Widget Formatting: Reformatting Dates with XPHP tools

1 Like