简体   繁体   中英

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. How do I solve the issue without using cookies and or sessions?

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

    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. And compare it in Js as well.

  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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