Hi I'm making an online coupon system for mobile devices. The coupons are for a limited time active and I have to do a check on the coupons. I use date(d/m/Y H:i:s) for just showing the date and time, and I also have a expire date which is a just a string that I later convert into a date.
This is how I do the check if the coupon is expired:
if ($date1B > $date2B) {
echo "<script>alert('Expired coupon!!');</script>";
}
Now what I want is to calculate the days when the coupon will be expired.
This is what I found on W3Schools, but the example below uses date_create(), so you make an custom date and time. I already have 2 dates and times.
$date1 = date("d-m-Y H:i:s");
$date2 = date_format($date2A, 'd-m-Y H:i:s');
$diff = date_diff($date1,$date2);
echo $diff->format("%a days %h Hours %i Minute %s Seconds ");
When I replace variables with the existing variables I get these errors:
date_diff() expects parameter 1 to be DateTimeInterface, string given in
and
Call to a member function format() on boolean in
The full .PHP page:
<?php
date_default_timezone_set("America/Curacao");
$date1A = date("d/m/Y H:i:s");
$date1B = date("dmYHis");
$date2B = "27032017042100";
$date2A = date_create_from_format('dmYHis', $date2B);
echo "Datum 1: " . $date1A . "<br>" ;
echo "Datum 1: " . $date1B . "<br><br>";
echo "Datum 2: " . date_format($date2A, 'd/m/Y H:i:s') . "<br>";
echo "Datum 2: " . $date2B . "<br>";
if ($date1B > $date2B) {
echo "<script>alert('Klaar!!');</script>";
}
$date1 = date("d/m/Y H:i:s");
$date2 = date_format($date2A, 'd/m/Y H:i:s');
$diff = date_diff($date1,$date2);
echo $diff->format("%a days %h Hours %i Minute %s Seconds ");
?>