• Resolved syoung68

    (@syoung68)


    Any way to format a date value. They are pulling as yyyy-mm-dd and I would rather display then as mm/dd/yyyy

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Phi Phan

    (@mr2p)

    Hi @syoung68
    There is a filter to change the output of the field’s content. You can add the following snippet code into your theme or plugin:

    add_filter(
    	'meta_field_block_get_block_content',
    	function ( $content, $attributes ) {
    		$field_name = $attributes['fieldName'] ?? '';
    		// TODO: input your field name here.
    		if ( 'YOUR_FIELD_NAME_HERE' === $field_name ) {
    			return date( 'm/d/Y', strtotime( $content ) );
    		}
    
    		return $content;
    	},
    	10,
    	2
    );

    I’m going to do a setting allowing to choose the output format of the block soon, but you can use the above code to display your field in the mean time.
    Please let me know if it works or not.
    If it works for you, please do a quick review for it. Thank you very much.
    Phi.

    Thread Starter syoung68

    (@syoung68)

    Thank you! Worked like a charm. I did make one change to account for empty fields…

    add_filter(
    	'meta_field_block_get_block_content',
    	function ( $content, $attributes ) {
    		$field_name = $attributes['fieldName'] ?? '';
    		// TODO: input your field name here.
    		if (( 'YOUR_FIELD_NAME_HERE' === $field_name ) && ($content != '')) {
    			return date( 'm/d/Y', strtotime( $content ) );
    		}
    
    		return $content;
    	},
    	10,
    	2
    );
    Plugin Author Phi Phan

    (@mr2p)

    Glad to hear it worked out. I’ve forgoten to do a empty check. You also can get the date_format from the settings like get_option(‘date_format’) instead of of harded-code ‘m/d/Y’. I think that’s better.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Format a Date Value’ is closed to new replies.