Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Formatting an ACF date for a loop item

  • Hello,

    In a query,I’m trying to format a date which is an ACF custom field of type “date” to french format. I managed to display the field with the loop item : {{loop_item key:date_debut_evt}}. This custom field is “date_debut_evt”.
    I’m using this filter :

    
    add_filter( 'generateblocks_dynamic_tag_output', function( $output, $options ) {
        if ( isset( $options['tag'] ) && $options['tag'] === 'loop_item' 
             && isset( $options['key'] ) && $options['key'] === 'date_debut_evt' ) {
            $datetime = DateTime::createFromFormat( 'Ymd', $output );
            return $datetime ? date_i18n( 'j F Y', $datetime->getTimestamp() ) : $output; // "29 avril 2026"
        }
        return $output;
    }, 10, 2 );
    

    But I can’t get it to work, the output date is always: 20260429 for example.

    Could you help me ?

    Eric.

  • Hi there,

    Can you try modifying your code to this?

    add_filter( 'generateblocks_dynamic_tag_output', function( $output, $options ) {
    
        if (
            isset( $options['tag'], $options['key'] ) &&
            $options['tag'] === 'loop_item' &&
            $options['key'] === 'date_debut_evt'
        ) {
    
            // Ensure value is a string like "20260429"
            $raw = trim( (string) $output );
    
            // Validate format strictly
            $datetime = DateTime::createFromFormat( 'Ymd', $raw );
    
            if ( $datetime instanceof DateTime ) {
                return date_i18n( 'j F Y', $datetime->getTimestamp() );
                // → 29 avril 2026 (if site language = French)
            }
        }
    
        return $output;
    
    }, 10, 2 );
    

    Let me know if this helps!

  • Hello Ying,

    Thanks for that code. I didn’t get a formatted date with it so I put a trace in the filter and it never appeared.

    By looking at the trace, I could see that it is not ‘tag’ in the Options array but tag_name.
    Now it seems ok, here’s the code :

    
    add_filter( 'generateblocks_dynamic_tag_output', function( $output, $options ) {
        if (
            isset( $options['tag_name'], $options['key'] ) &&
            $options['tag_name'] === 'loop_item' &&
            $options['key'] === 'date_debut_evt'
        ) {
            // Ensure value is a string like "20260429"
            $raw = trim( (string) $output );
    	
            // Validate format strictly
            $datetime = DateTime::createFromFormat( 'Ymd', $raw );
    
            if ( $datetime instanceof DateTime ) {
                return date_i18n( 'j F Y', $datetime->getTimestamp() );
                // → 29 avril 2026 (if site language = French)
            }
        }
    
        return $output;
    
    }, 10, 2 );
    

    Eric.

  • Hi Eric.

    Please, use this code instead:

    add_filter( 'generateblocks_dynamic_tag_output', function( $output, $options ) {
        // Check tag_name instead of tag
        $tag_name = $options['tag_name'] ?? '';
        
        if ( 'loop_item' === $tag_name && isset( $options['key'] ) && $options['key'] === 'date_debut_evt' ) {
            $datetime = DateTime::createFromFormat( 'Ymd', trim( $output ) );
            if ( $datetime instanceof DateTime ) {
                return date_i18n( 'j F Y', $datetime->getTimestamp() );
            }
        }
        
        return $output;
    }, 10, 2 );
  • Hello George,

    Tested and working ok. Thanks.

    Eric.

  • Excellent, no problem!

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.