Change date format in Choice Tab

Forums › Forums › Search & Filter Pro › Change date format in Choice Tab

Viewing 10 posts – 1 through 10 (of 11 total)
  • Anonymous
    #229302

    I have a ACF fields that are dates and are formatted to read as “Mon Day Year” or F j.
    Where can I change the formatting from YYYYMMDD? I’m sorry if I have missed it somewhere.

    Trevor
    #229341

    This is the format (Ymd) that is need for our plugin to work with the ACF date fields:

    https://www.screencast.com/t/Rk48ZZQ0VR

    Anonymous
    #229394

    SO I cannot edit through PHP the return format for this:

    Publication Date Sample

    to read : Dec 10, 2019

    in the dropdown list?
    ??

    Trevor
    #229396

    At the moment, these are the options:

    https://www.screencast.com/t/eW7UqRQbIP

    Otherwise, you would need to change the appearance using this filter:

    https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object

    Anonymous
    #229407

    I’ve tried working with the filter but I can only edit basic (cut/paste/replace) PHP, that whatever filter I apply it keeps breaking the site. I am probably scripting the code wrong as well.

    I tried also applying what I’ve learned in these forums
    https://www.javatpoint.com/how-to-change-date-format-in-php
    https://learncodeweb.com/php/change-date-format-in-php/
    https://tecadmin.net/convert-date-format-in-php/

    It’s a great plugin and it answers most (if not all) of my current issues with other filter plugins.

    Trevor
    #229703
    This reply has been marked as private.
    Ross Moderator
    #230046

    Hi Yanin

    So I’m trying to understand the scenario.

    Is is that, although the filtering works with the date field, you want to display it in a better format?

    If so, I can knock up some code for you to copy and paste to do what you need.

    Thanks

    Anonymous
    #230049

    Yes. I would like to display the date differently as mentioned above that would be very helpful.

    Ross Moderator
    #230093

    Hi Yanin

    This code should do the trick:

    
    function sf_format_date_labels($input_object, $sfid){
    
    	//change <code>_sfm_date</code> to the name of your field
    	if($input_object['name']=='_sfm_date')
    	{
    		//udpate this field before rendering
    		if(!isset($input_object['options']))
    		{
    			return $input_object;
    		}
    		
    		//now we know there are options we can loop through each one, and change what we need
    		foreach($input_object['options'] as $option)
    		{
    			if($option->value=="")
    			{//the option with no value is always the "all items" or unselected state
    				//$option->label = "This could be a default label";
    			}
    			else
    			{
    				$date = $option->value;
    				
    				//convert date to YYYY-MM-DD so strtototime doesn't mix up months / days
    				$input_date = substr($date, 0, 4) . '-' . substr($date, 4, 2). '-' . substr($date, 6, 2);
    				
    				//now format the date how we want
    				$date_format = "M j, Y";
    				$option->label = date($date_format, strtotime($input_date));
    			}
    		}
    	}
    	
    	return $input_object;
    }
    add_filter('sf_input_object_pre', 'sf_format_date_labels', 10, 2);

    Things to note:
    1) change _sfm_date to the name of your field – you can see the name of your field by checking it in the URL, after performing a search with a date value selected
    2) To change the display format, modify the line:

    
    $date_format = "M j, Y";
    

    You can format the date using the normal PHP date parameters: https://www.php.net/manual/en/function.date.php

    Let me know how you get on.

    Thanks

    Anonymous
    #230119

    Thank you! This is working great!

Viewing 10 posts – 1 through 10 (of 11 total)
Search & Filter Support
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

We also use cookies to store items in your cart as well as allowing your to login on the site.

You can adjust all of your cookie settings by navigating the tabs on the left hand side.

By continuing to use this site, you also agree to our Privacy Policy.