将用户时区(世界上任何地方)与当前太平洋+一小时的PHP进行比较


compare user time zone (anywhere in the world) to current Pacific+one hour in PHP

该项目包括为用户创建一个表格,以输入促销的开始和结束时间。促销活动的现场网站在太平洋时区运营,创建促销活动的用户可以在世界任何地方。

开始时间必须比当前PST(或PDT,取决于季节)长一小时。当前验证开始时间的方法不起作用,因为它提取了用户计算机的本地时间。

我需要一种方法来将用户的本地时间与太平洋时间进行比较,并验证促销开始时间是否延长了一个小时。

我的工作理论是找到用户的本地时间和GMT时间之间的偏移量,然后找到当前太平洋时间和GMT之间的偏移(根据夏令时的不同,偏移量会变化7或8个小时,对吧?),然后将这些偏移量应用于用户的时间,并与太平洋时间加一小时进行比较。

我已经成功地找到了必要的偏移量,并在各种字符串和时间戳中提醒太平洋时间中的正确当前时间,但总体逻辑让我无法理解。此外,我还无法成功地将一个小时添加到时间戳中。

这个问题和许多其他问题类似,但在这种情况下,OP有一个固定的偏移:比较用户';s与网站的时区';s办公地点时区

当前代码:

function valid() {
var starttime=$('#1-PromotionalSaleStartTime').val();
var endtime=$('#1-PromotionalSaleEndTime').val();
    var now = new Date();
    var hour= now.getHours();
    var min = now.getMinutes()+10;
    var nows= parseInt(hour)+1;
    var time=nows+':'+min;     
    var presentime = now.getHours()+':'+now.getMinutes()
    var month =now.getMonth()+1;
    var day = now.getDate();
    var output = (month<10 ? '0' : '') + month + '/' +(day<10 ? '0' : '') + day + '/'  + now.getFullYear() + ' '+time;
    var now    = (month<10 ? '0' : '') + month + '/' +(day<10 ? '0' : '') + day + '/'  + now.getFullYear() + ' '+presentime;
    var present    = new Date(now);
    var oneDay     = 24*60*60*1000;    // hours*min*sec*milliseconds
    var firstDate  = new Date(starttime);        
    var secondDate = new Date(endtime);
    var diffDays   = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));
    var diff       = Math.round(Math.abs(( present.getTime() - firstDate.getTime())/(oneDay)));
    var presentTimeStamp = +'<?php echo time(); ?>' * 1000;
    var firstDateTimeStamp = Date.parse($('#1-PromotionalSaleStartTime').val());
var err        = 0;

<?php if($this->add!="" && isset($this->add)) {?>
if(presentTimeStamp > firstDateTimeStamp) {
$('#1-PromotionalSaleStartTime').after('<ul class="errors"><li>Sorry, but you cannot add past date.</li></ul>');
        err++;

}

<?php } ?>
    if(diffDays==0){
             $('#1-PromotionalSaleEndTime').after('<ul class="errors"><li>The date difference between Start and End dates should be 24 hours.</li></ul>');
    err++;
     }
    if(starttime < output){
              $('#1-PromotionalSaleStartTime').after('<ul class="errors"><li>Your Start time should be at least 1 hour more than the current Pacific Time like. '+ output +'</li></ul>');
    err++;
}
if((Date.parse(starttime)> Date.parse(endtime)) ){
    $('#1-PromotionalSaleEndTime').after('<ul class="errors"><li>End Time cannot be less than Start Time plus 1 day.</li></ul>');
    err++;
}

在服务器端尝试此操作:

/* timetest.php */
<?php
if(isset($_POST['dateInfo'])){
  date_default_timezone_set('America/Los_Angeles'); $data = array();
  $dt = new DateTime;
  $pacificPlusOneOffset = dt->add(new DateInterval('P1D'))->getOffset();
  $data['diff'] = +$_POST['dateInfo']-$pacificPlusOneOffset;
  echo json_encode($data);
}
else{
  // could be some kind of hack
}
?>

AJAX应该在客户端发送JavaScript,比如:

var pre = onload;
onload = function(){
if(pre)pre();
// more code to run other code here
function post(url, send, success){
  var x = new XMLHttpRequest || new ActiveXObject('Microsoft.XMLHTTP'), ec = [], s;
  for(var i in send){
    ec.push(encodeURIComponent(i)+'='+encodeURIComponent(send[i]));
  }
  s = ec.join('&').replace(/%20/g, '+'); x.open('POST', url);  
  x.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  x.setRequestHeader('Content-length', s.length);
  x.setRequestHeader('Connection', 'close');
  x.onreadystatechange = function(){
    if(x.readyState === 4 && x.status === 200 && success){
      success(eval('('+x.responseText+')'));
    }
  }
  x.send(s);
}
post('timetest.php', {dateInfo:new Date().getTimezoneOffset()}, function(result){
  console.log(result.diff);
})
}