Forums › Forums › Search & Filter Pro › Change date format in Choice Tab
- This topic has 10 replies, 3 voices, and was last updated 4 years, 10 months ago by Trevor.
-
Trevor(Private) December 18, 2019 at 4:06 pm #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(Private) December 18, 2019 at 6:15 pm #229407I’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.
Ross Moderator(Private) December 31, 2019 at 1:28 pm #230046Hi 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
Ross Moderator(Private) January 2, 2020 at 4:35 pm #230093Hi 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
-
AuthorPosts