20

I have created an event post type in Wordpress. For that I have put starting date and ending date from ACF datepicker.

I want admin can select Ending date greater than Starting Date.

Is there any way for restricting Starting Date and Ending Date?

For example, if Admin choose 1st Jan 2016 as starting date, then he can only select the ending date 1st Jan or greater then the selected date.

Jitendra Damor
  • 774
  • 17
  • 27

4 Answers4

4

I think we can do it with java script and use this code to set the limit of the end date :

$( ".selector" ).datepicker({
  minDate: new Date(  )
});
Ahmed Bltagy
  • 114
  • 3
  • I want to restrict the starting and ending date in wordpress admin panel. If admin select some date for starting date then the ending date should be equal or greater then the starting date. – Jitendra Damor Apr 08 '15 at 08:11
  • Yes I need some please help me I also https://support.advancedcustomfields.com/forums/topic/restrict-datepicker-ending-date-by-starting-date/ but not resolved my issues – Anand Choudhary Apr 16 '18 at 12:51
4

I think there is no possibilities for date restriction in acf in admin area.

I may be done in acf's newer version.

You can request from here...

http://support.advancedcustomfields.com/forums/forum/feature-requests/

Jitendra Damor
  • 774
  • 17
  • 27
3

I had similar problem with regular date fields, Hope this JS code (with the moment JS library) with some adjustments will help you.

$(document).ready(function() {
   $("input[name='Arrival']").change(function() {
      var date_picked = $("input[name='Arrival']").val();
      var SpecialTo = moment(date_picked, "YYYY-MM-DD");
      var today = new Date();
      today.setDate(today.getDate() - 240);
      var selectedDate = new Date(date_picked);
      if (today <= selectedDate) {
         //alert('Date is today or in future');
      } else {
         alert('Date is in the past');
         $("input[name='Arrival']").val('');
      }
  });
}) 

If you could post the source HTML of the date input with a value, I could change it probably to what you looking for.

BenB
  • 2,747
  • 3
  • 29
  • 54
1

This works just fine. Just get the name fields with inspect element. Add this code in the functions.php file.

add_action('acf/validate_save_post', 'my_acf_validate_save_post', 10, 0);

/**
 * @throws Exception
 */
function my_acf_validate_save_post() {

    $start = $_POST['acf']['field_61a7519a57d99'];
    $end = $_POST['acf']['field_61a751d957d9a'];

    // check custom $_POST data
    if ($start > $end) {
        acf_add_validation_error('acf[field_61a751d957d9a]', 'End date should be greater than or equal to start date.');
    }
}

When you open inspect element the input field should look like this:

<div class="acf-date-picker acf-input-wrap" data-date_format="dd. MM yy" data-first_day="1">
     <input type="hidden" id="acf-field_61a751d957d9a" name="acf[field_61a751d957d9a]" value="20211201">
     <input type="text" class="input hasDatepicker" value="16. January 2022" id="dp1638477022818">
</div>

More information you can find here: https://www.advancedcustomfields.com/resources/acf-validate_save_post/