Formatting PHP output for overdue invoices

Hi, I’m trying to figure a way of making a list of invoices on a user’s dashboard page color-coded. Basically if an invoice becomes overdue then the output changes to orange, and if it’s more than 30 days past the invoice date then it changes to red.

So I tried to calculate this based on three things- the date of the invoice, the due date, and whatever the date is on the day, thinking that should be enough.

The invoice date and due date are pulled from the MySQL table thus:

  $invoicedate = $row["InvoiceDate"];
  $duedate = $row["InvoiceDueDate"];

And I then tried to set up a condition for having the output formatted:

  $today = date('Y-m-d');
  
  if ($duedate < $today) {echo '<div style="font-weight:bold; color:#ff0;">';}
  else {echo '<div style="font-weight:normal; color:#000;">';}

And obviously the div is closed further down in the loop.

I’m thinking the condition just isn’t working though as I can’t get it to format. The above code is for when an invoice becomes overdue (i.e the due date is now older than today’s date). But I also need one for when the due date becomes 30 days older than today’s date.

What would be the best way to achieve this? Thanks!

The problem is that you’re actually comparing strings that represent the dates, rather than the dates themselves. One way to do this is to create DateTime objects from the dates first, then the comparison will work:

$duedate = new DateTime($row["InvoiceDueDate"]);
$today   = new DateTime('now');

if ($duedate < $today) {
   // etc
}

Great, got that working now. Thanks!

However, how would I get this to work to also calculate and flag up a color if today’s date is more than 30 days on from the due date?

@speckytwat,

However, how would I get this to work to also calculate and flag up a
color if today’s date is more than 30 days on from the due date?

you can use this.

http://php.net/manual/en/datetime.diff.php

Then you can use the diff() method to calculate the interval between the two dates:

$interval = $duedate->diff($today);

if ($interval->days > 30) {
    // ...
}

That works :slight_smile: Thanks.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.