简体   繁体   English

如何在页面加载时在Javascript变量和php值之间制作php IF语句而不提交表单

[英]How to make php IF statement between Javascript variable and php value on page load without submitting form

I have to get Browser Local date and time with Javascript and compare it in PHP IF statement with value from database on page load to determine if product expired or not. 我必须使用Javascript获取浏览器本地日期和时间,并在PHP IF语句中将其与页面加载时数据库中的值进行比较,以确定产品是否已过期。 How do I solve the issue without using cookies and or sessions? 如何在不使用cookie和/或会话的情况下解决问题?

Javascript 使用Javascript

 <script> 

      var currentDate = new Date(),
      day = ('0' + (currentDate.getDate()+1)).slice(-2),
      month = ('0' + (currentDate.getMonth()+1)).slice(-2),
      year = currentDate.getFullYear();
      var currentTime = new Date(),
      hours = currentTime.getHours(),
      minutes = currentTime.getMinutes();

    if (minutes < 10) {
     minutes = '0' + minutes;
  }   

 document.write(day + '/' + month + '/' + year + ' ' + hours + ':' + minutes)

</script>

PHP PHP

    if(strtotime( date and time from SCRIPT ) < strtotime($events date and time exiptation as $events_total_SKU[$e]['sales_end'])) 
{   
   print(" OK ");
 .... and  shopping cart code
}
     else{
   print(" Expired ");
}

You should store both the dates in JavaScript variables as below. 您应该将这两个日期存储在JavaScript变量中,如下所示。 And compare it in Js as well. 并在Js中进行比较。

  var currentDate = new Date(),
  day = ('0' + (currentDate.getDate()+1)).slice(-2),
  month = ('0' + (currentDate.getMonth()+1)).slice(-2),
  year = currentDate.getFullYear();
  var currentTime = new Date(),
  hours = currentTime.getHours(),
  minutes = currentTime.getMinutes();


  var frontendDate = year + '-' + month + '-' +day + ' ' + hours + ':' + minutes;


  var backednDateString = "<?php echo $events_total_SKU[$e]['sales_end'];?>";
  // As backedn date is string convert it as date first.
  var backednDateS  = new Date(backednDateString );

if(frontendDate  < backednDate) 
{   
   alert(" OK ");
}
else{
   alert(" Expired ");
}

Make sure format of both the dates are same. 确保两个日期的格式相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM