Berk Balik
Berk Balik

Reputation: 91

Calculating the difference between dates not working and giving an huge error

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 ");
?>

Upvotes: 0

Views: 1183

Answers (3)

Berk Balik
Berk Balik

Reputation: 91

Updated .PHP file

I fixed the errors.. This is the updated .PHP page:

<?php

    date_default_timezone_set("America/Curacao");
    $date1A = date("d-m-Y H:i:s");
    $date1 = date_create($date1A);
    echo date_format($date1,"d-m-Y H:i:s");

    echo "<br>";

    $date2B = "31032017042100";
    $date2A = date_create_from_format('dmYHis', $date2B);
    $final = date_format($date2A, 'd-m-Y H:i:s');
    $date2 = date_create($final);
    echo date_format($date2,"d-m-Y H:i:s");

    echo "<br>";

    $diff = date_diff($date1, $date2);
    echo $diff->format("%R %a days %h Hours %i Minute %s Seconds");

    if ($date1 > $date2) {
       echo "<script>alert('Coupon Expired!!');</script>";
    }

?>

Upvotes: 1

Ju Oliveira
Ju Oliveira

Reputation: 428

Based on @ShaktiPhartiyal answer, to output in the format that you want, just use:

$startDate = date_create('2014-06-13');
$endDate = date_create('2017-08-10');
$diff = date_diff($startDate, $endDate);
echo $diff->format("%a days %h Hours %i Minute %s Seconds ");

And the output will be:

1154 days 0 Hours 0 Minute 0 Seconds

Upvotes: 0

Shakti Phartiyal
Shakti Phartiyal

Reputation: 6254

The simplest way to calculate the difference between two dates is date_diff() function which gives error on invalid dates.

Before passing on the values to date_diff function you should use the date_create() function.

Usage:

<?php
$startDate = date_create('2014-06-13');
$endDate = date_create('2017-08-10');
$diff = date_diff($startDate, $endDate);
var_export($diff);

Which gives you output like:

DateInterval::__set_state(array( 'y' => 3, 'm' => 1, 'd' => 28, 'h' => 0, 'i' => 0, 's' => 0, 'weekday' => 0, 'weekday_behavior' => 0, 'first_last_day_of' => 0, 'invert' => 0, 'days' => 1154, 'special_type' => 0, 'special_amount' => 0, 'have_weekday_relative' => 0, 'have_special_relative' => 0, ))

UPDATE

You can use or modify the following function to get the difference:

function dateDifference($startDate, $endDate)
{
    try {
        $startDate = date_create($startDate);
        $endDate = date_create($endDate);
        $diff = date_diff($startDate, $endDate);
        $d = "";
        if ($diff->y != 0) {
            if ($diff->y > 1) {
                $d .= $diff->y . " Years ";
            } else {
                $d .= $diff->y . " Year ";
            }
        }
        if ($diff->m != 0) {
            if ($diff->m > 1) {
                $d .= $diff->m . " Months ";
            } else {
                $d .= $diff->m . " Month ";
            }
        }
        if ($diff->d != 0) {
            if ($diff->d > 1) {
                $d .= $diff->d . " Days ";
            } else {
                $d .= $diff->d . " Day ";
            }
        }
        if ($diff->h != 0) {
            if ($diff->h > 1) {
                $d .= $diff->h . " Hours ";
            } else {
                $d .= $diff->h . " Hour ";
            }
        }
        if ($diff->i != 0) {
            if ($diff->i > 1) {
                $d .= $diff->i . " Minutes ";
            } else {
                $d .= $diff->i . " Minute ";
            }
        }
        if ($diff->s != 0) {
            if ($diff->s > 1) {
                $d .= $diff->s . " Seconds ";
            } else {
                $d .= $diff->s . " Second ";
            }
        }
        return $d;
    }
    catch(Exception $e)
    {
        die("ERROR");
    }
}

Running it like so:

echo dateDifference('2014-10-16', '2017-06-08');

shall output:

2 Years 7 Months 23 Days

Upvotes: 0

Related Questions