Jquery日期选择器:中午后禁用第二天

时间:2015-11-20 11:37:20

标签: jquery wordpress datepicker gravity-forms-plugin

我正在使用Gravity Forms和Gravity Forms WooCommerce Product Add-Ons插件向产品添加交付日期。

我希望客户能够选择第二天如果是在中午12点之前,而不是在中午之后(产品仅在英国​​提供)。

这是我用来使以前的日期变灰并且也禁用星期日的原因,因为周日没有送货:

<script type="text/javascript">
jQuery(document).ready(function($) { 
$(".datepicker").datepicker({ minDate:1, beforeShowDay: function(date) 
{ var day = date.getDay(); return [(day != 0), '']; } }); });
</script>

我发现了这个问题/答案:JQuery UI Datepicker Disbale Next Day After 12pm

但是我不确定如何将我需要的代码与该解决方案隔离,以便在中午之后禁用次日发送,而不是添加所有其他功能。如果有人能够帮助我,我将非常感激!

2 个答案:

答案 0 :(得分:0)

首先,你需要获得英国的当前时间。 然后你需要验证星期日和正午时间。如果当前时间正常(根据您的条件),则需要设置(可见)datepicker。

创建文件getUKTime.php

    <?php 
    $userCountry = getCountryCode();
    if($userCountry=='GB') {
        date_default_timezone_set('Europe/London');
        $data=array("message"=>"success", "date"=>date("l Y-m-d g:i a"));
        echo json_encode($data);
    }else {
        $data=array("message"=>"The products are only available in the UK!");
        echo json_encode($data);    
    }
    /* get current user location iformation that either the user from UK or not */
    function getCountryCode() {
        $headers = array('Content-Type: application/json');
        $ip = $_SERVER["REMOTE_ADDR"];
        $deep_detect = TRUE;
        if ($deep_detect) {
          if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP)){
             $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
          }
          if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP)){
             $ip = $_SERVER['HTTP_CLIENT_IP'];
          }
        }
        $url = 'http://www.geoplugin.net/json.gp?ip='.$ip;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $result1 = curl_exec($ch);
        curl_close($ch);        
        $data=json_decode($result1, true);
        return $data['geoplugin_countryCode'];
    }
    ?>

在代码中编辑jquery函数

    <script>
        var midday = 0;// default value is 0 for diabled datepicker selction
        $.ajax({
               url: "getUKTime.php",
               type: "POST", data: {},
               beforeSend: function (jqXHR, settings){if (jqXHR && jqXHR.overrideMimeType) {jqXHR.overrideMimeType("application/j-son;charset=UTF-8");}},
               dataFilter: function (data) {var response = eval('(' + data + ')');return response;},
               success: function (result) {
                   if(result.message=='success') {
                       var getDates=result.date.split(" ");
                       if(getDates[3]=='pm'){  midday=1;/*set it '1' to enable datepicker (UK time is in "pm" then datepicker will be enable)*/ }
                   }else{
                      midday=0;
                      alert(result.message); 
           }
                   $( "#Datepicker1").datepicker({
                       minDate: 0,
                       dateFormat: 'mm/dd/yy',
                       inline: true,
                       dayNamesMin: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
                       beforeShowDay: disableDays,
                    });
               },
               error: function (jqXHR, textStatus, errorThrown) {},
               complete: function (jqXHR, textStatus){}
            }); 

        function disableDays(date){ 
              if(midday==0) {return [(day > 0), ''];}
              var day = date.getDay();
              return [(day > 0), ''];
          }; 
    </script>
    <input type="text" id="Datepicker1" />

答案 1 :(得分:0)

我将此策略用于本地接送日期选择器选项。它已经在多个客户端站点上运行了几个月,没有任何关于准确性的投诉!

<script>

    function partial_day_cutoff(date) {

      // default to no orders in the same day
      var minDate = 1;

      // get the current date stamp
      var date = new Date();

      // get the current timestamp (military)
      var current_hour = date.getHours();

      // if the current timestamp is greater than 12, 
      //change the minDate to no orders within this and the next day
      if( current_hour > 12 ) {
          minDate = 2;
      }

      return minDate;

    }


    jQuery(document).ready(function($){

      var minDate = partial_day_cutoff();

      $("#thedate").datepicker({

        minDate : minDate,

      });   

    });

</script>