How to format the output of a date field?

I’m rendering a dynamic data output for an event CPT provided by Modern Event Calendar plugin.
by default, the metakey: {cf_mec_start_date} displays the event start date in this format: 2023-01-04
But I would like a more human friendly format like January 4, 2023.
From my research so far, I think its possible to create a function that modifies the output format. any help would be appreciated.

Hello,

Did you find a solution ?

Regards,

Nicolas.

@nickwe - the easiest way is to use a php function… for example:
{echo:my_function({custom_field})}

And in functions.php
function my_function($thedate) {
$returndate = date_format($thedate,“Y/m/d H:i:s”);
return $returndate;
}

You can format the date however you need.

2 Likes

Thank you so much for pointing me into the right direction.

For an unknown reason, I had to add a line with date_create before the date_format like this to make it work:

function my_function($thedate) {
$date=date_create($thedate);
$returndate = date_format($date,"Y/m/d H:i:s");
return $returndate;
}

And then use the CustomField from Eventin inside a text element like this:

{echo:my_function({cf_etn_start_date})}

Edit: To make it work with locale, we need to use strftime:

function my_function($thedate) {
setlocale(LC_TIME, "fr_FR");
$date=date_create($thedate);
$formatted_time = strftime("%d %B %Y", $date->getTimestamp());
return $formatted_time;
}

Regards.

2 Likes

Code is basically hope and witchcraft where everyone’s borrowing each-others spells!
Glad you got it working.

2 Likes

Used this in combination with @nickwe 's date_create addition, and it worked for me.

I’ve been scouring the facebook group and several different tutorials. This is the only one that worked for me.

None of those examples worked for me when trying to simply return a simple number format for post dates. Here’s what worked for me inside query loops:

// Add into Bricks text fields like: {echo:digit_date()}
function digit_date() {
    $formatted_date = get_the_date('n/d/y');
    return $formatted_date;
}

It’s even easier than that!

Simply add the format code that you want at the end of the custom field ID…
{acf_event_date_time:m/d/Y} is 02/01/2024
{acf_event_date_time:F j, Y} is February 1, 2024
{acf_event_date_time:M j, Y} is Feb 1, 2024
You can add time too
{acf_event_date_time:M j, Y g:ia} is Feb 1, 2024 10:00 am

Very useful for formatting cards and events where you need to have more or less info depending on the space you have.

2 Likes

hi, it doen’t works for me with acf, works with {current_date:D j F Y}. thx

Best answer. it works perfectly for post_date. No functions needed.

{post_date: F Y} gives “March 2024”

1 Like

The date formatting filters are not working on custom fields for me (e.g., {cf_my_date_field:F j, Y}).

How can we tell Bricks this is a date field, and that date filters should work?

Thanks!