0

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 ");
}
FedeP
  • 1
  • 1
  • Possible duplicate of [jQuery Ajax POST example with PHP](https://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php) – Xun May 31 '19 at 07:17
  • Why do you need this to be based on my local time to begin with? If I see that the product I want just “expired” minutes ago for me in my time zone, then what is supposed to stop me from simply changing the time zone on my system, to get access to your product again …? – 04FS May 31 '19 at 07:39
  • Hi 04FS you perfectly right but the product is a Events tickets and pre-sales end at certain time and date. The site operates internationally and I need to close the sale on Event local time not on server time. Any ideas? – FedeP May 31 '19 at 07:47

1 Answers1

0

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.

Rahat Hameed
  • 412
  • 3
  • 16
  • Hi Rahat thanks al lot for quick answer. Problem is I have inside PHP IF statement a whole lot of code to be executed if sale not expired instead of just an alert. I edited the PHP code accordingly – FedeP May 31 '19 at 07:31