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

时间:2014-10-24 02:06:06

标签: php validation datetime

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

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

我需要一种方法来将用户当地时间与太平洋时间进行比较,并验证促销开始时间是否超过一小时。

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

我成功地找到了必要的偏移并在太平洋时间以各种字符串和时间戳警告正确的当前时间,但总体逻辑让我感到惊讶。此外,我无法成功地向TimeStamp添加一个小时。

这个问题与其他许多问题类似,但在这种情况下,OP有一个固定的偏移量: Compare user's time zone with the website's office location time zone

当前代码:

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++;
}

1 个答案:

答案 0 :(得分:0)

在服务器端尝试此操作:

/* 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);
})
}