Support

Account

Home Forums Feature Requests Option to format Date & Time pickers from the core WordPress options.

Helping

Option to format Date & Time pickers from the core WordPress options.

  • It would be great if we can get a ‘WordPress default’ option added to display_format and return_format settings for Date Picker, Time Picker and Date Time Picker fields. Something like:

    Example of admin UI

    This field would then format the date/time from the users preferences found in Settings > General.

    Reason being is we create a number of multilingual sites using WordPress Multisite (Network) of which all use the same theme and ACF fields (using ACF JSON). Each site has it’s own date/time formatting depending on the locale.

    I have solved this by filling the custom format setting with “WORDPRESS” like so…

    Example of current solution.

    And modifying the fields with this snippet…

    
    add_filter( 'acf/load_field/type=date_picker', function( $field ) {
    	$date_format = get_option( 'date_format' );
    
    	if ( $field[ 'display_format' ] === 'WORDPRESS' ) {
    		$field[ 'display_format' ] = $date_format;
    	}
    
    	if ( $field[ 'return_format' ] === 'WORDPRESS' ) {
    		$field[ 'return_format' ] = $date_format;
    	}
    
    	return $field;
    }, PHP_INT_MAX, 1 );
    
    add_filter( 'acf/load_field/type=time_picker', function( $field ) {
    	$time_format = get_option( 'time_format' );
    
    	if ( $field[ 'display_format' ] === 'WORDPRESS' ) {
    		$field[ 'display_format' ] = $time_format;
    	}
    
    	if ( $field[ 'return_format' ] === 'WORDPRESS' ) {
    		$field[ 'return_format' ] = $time_format;
    	}
    
    	return $field;
    }, PHP_INT_MAX, 1 );
    
    add_filter( 'acf/load_field/type=date_time_picker', function( $field ) {
    	$datetime_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' );
    
    	if ( $field[ 'display_format' ] === 'WORDPRESS' ) {
    		$field[ 'display_format' ] = $datetime_format;
    	}
    
    	if ( $field[ 'return_format' ] === 'WORDPRESS' ) {
    		$field[ 'return_format' ] = $datetime_format;
    	}
    
    	return $field;
    }, PHP_INT_MAX, 1 );
    

    I think this would be beneficial for a number of users. Just a thought.

    Thanks!

  • Interesting solution.

    If I was going to to this I would just ignore the settings for the fields. I would then create 2 filter.

    The first would be an acf/prepare_field filter. In this filter I would alter the display format and simply set all date and date/tie fields the WP format.

    The second filter would be an acf/format_value filter with a priority > 10 so that it runs after the ACF built in filter. Here I would call this with the WP date format value

    
    return acf_format_date( $value, 'your_format_here');
    

    This could also be used in conjunction with your custom setting if you did not want to do this for all date fields, but if I was going to do this it would all be the same.

    Actually to be honest, I set all date field return values to ‘Y-m-d’ and all date time fields to ‘Y-m-d H:i:s’ because I generally custom code all date display on the front end to meet client needs and those needs usually mean that I’ll be showing dates differently depending on where they will be shown. For example an event might actually show something like June 1 to June 4, 2021 (start and end dates) and this cannot be achieved without additional coding. These return formats are the easiest to use when custom formatting dates and easy enough to change.

    
    echo date('whatever format I want', strtotime(get_field('my-date-field')));
    

    So I would only concern myself with the acf/prepare_field filter.

    As an added note. Using the acf/prepare_field filter is usually always better than the acf/load_field filter. For example, in the field group editor if you edit the field group the load field filter could effect the field when editing it and you’ll need to set it back to your custom value every time you edit the group unless you conditionally alter the field based on the post type being loaded. It can also effect how the field is saved in local JSON and when exporting or importing a field group. So you’ll want to watch out for that.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.