0

I want to compare two dates; a receipt date, and a deployment date which has 14 days added to it.

If the receipt date is more than the deployment date, then the data should be highlighted red otherwise it should be highlighted black, and I managed to do this comparison but it only compares days and not years or months. Do I need to make these two dates into strings to compare them?

        while ($stmt->fetch()){
    if ($date < $deployment_date){

    echo '<font color="red">';
    }
    else{
        echo '<font color="black">';
    }


    echo "<table border='1' style='width:50%'>";
    echo "<td>";
    echo "<b>Receipt ID: <a href ='transactiondetail.php?receipt=$receipt'>$receipt</b></a>";
    echo "<br><br>";
    echo "Used By: $officer_id";
    echo "<br><br>";
    echo "Cost: $cost";
    echo "<br><br>";
    echo "Area travelled: $area";
    echo "<br><br>";
    echo "Date of Submission: $date ";

    $deployment_date = date_create("$deployment_date");        
    date_add($deployment_date, date_interval_create_from_date_string('14 days'));
    echo date_format($deployment_date, 'Y-m-d');        
    echo "<br><br>";

    echo "<br><br>";
    echo "</td>";
    echo '</font>';

    echo "</table>";
    echo "<br>";

    }
AbsoluteSpace
  • 710
  • 2
  • 11
  • 21
Wen Qing
  • 109
  • 6
  • Possible duplicate of [Compare given date with today](https://stackoverflow.com/questions/2113940/compare-given-date-with-today) – IcedAnt Jan 25 '18 at 06:46

2 Answers2

0

For date compare please try to use strtotime().

Here is an example:

$date = strtotime($date) & $deployment_date=strtotime($deployment_date)

And then try to compare.

galoget
  • 722
  • 9
  • 15
DiggV
  • 1
  • 2
0

The DateTime class lets you use the standard comparison operators.

For example,

$receiptDate = DateTime::createFromFormat('Y-m-d', '2000-01-01');
$deploymentDate = DateTime::createFromFormat('Y-m-d', '2001-01-01');

if ($receiptDate < $deploymentDate) {
    // do stuff here
}
James
  • 119
  • 1
  • 5