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;
}