首先您需要获取英国的当前时间。
然后在您需要验证周日和中午时间之后。如果当前时间没问题(根据您的条件),那么您需要设置(可见)日期选择器。
创建文件 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" />