ACF Date Field Value Custom Format in Bricks

In the Bricks forum, a user asks:

Hi there, I am setting up an events calendar using bricks.

In some circumstances I would like to display the date of the event as just ‘21 Dec’ and in other places with the year too and maybe written day i.e. ‘Friday 21 Dec 2022’ .

I have created a custom field with acf for the date and it works great in my repeater on the front end, but how can I change the output of the date on a case by case basis?

Simplified I’d like to do something like {acf_date:d j}

This Pro tutorial shows how we can define a custom function and use that with the Dynamic data feature of Bricks to output any Advanced Custom Fields‘ Date-type of field in the specified format.

Step 1

Edit the Date Picker-type of field in your field group.

Set Return Format to Ymd.

Save changes.

Step 2

Add the following in child theme‘s functions.php (w/o the opening PHP tag) or a code snippets plugin:

// Function to return the given ACF date field in the specified format
function bl_get_acf_date( $field, $format = 'F j, Y' ) {
	$date_string = get_post_meta( get_the_ID(), $field, true );

	if ( $date_string ) {
		// Create DateTime object from value (formats must match).
		$date = DateTime::createFromFormat( 'Ymd', $date_string );

		return $date->format( $format );
	}

	return '';
}

Set your desired default date format in

$format = 'F j, Y'

Date format reference

Step 3

To output a date field in the default format set in the function above:

{echo:bl_get_acf_date(banner_start)}

where banner_start is the name of the custom field.

Sample output: December 14, 2022

Another example:

{echo:bl_get_acf_date(banner_start, Y-m-d)}

outputs 2022-12-14.

Another example:

{echo:bl_get_acf_date(banner_start, l j M Y)}

outputs Wednesday 14 Dec 2022.

Reference

https://www.advancedcustomfields.com/resources/date-picker/#template-usage