比较两个数据库条目并生成span类

I wonder if anyone could help me with this.

I have two fields in a database that are shown using:

1-<?php echo JHTML::_('date', $row->created_date, 'd/m/Y'); ?>

2-<?php echo $row->delivery_date; ?>

The first one, is written to the database as a full date with time, and as you can see above, I strip the output to only show the date. In the database it would appear as '2013-09-10 11:56:52'

The second is from a text entry form field, that has just text saved to the database in the format d/m/Y. In the database this appears as '19/09/2013'

Is there a way I can produce an if statement, that will add a span and class tag around the 2nd line if this condition is true:

"If the $row->delivery_date is within 21 days after (and including) $row->created_date."

Would it ultimately be best if I made them both full date values? Would that make it easier to calculate how many days apart they are?

  1. Convert Dates to a common format using strtotime() (e.g. PHP works nicely with unix timestamp's):

    $created_date = strtotime($row->created_date); // These are basically seconds $deliver_date = strtotime($row->delivery_date);

  2. Calculate the difference between the two and covert it to days (each day is 86400 seconds):

    $days = ceil(abs($deliver_date - $created_date) / 86400);

  3. Check $days in your if() statement for echoing your span.

Something like:

if($days <= 21)
{
    echo '<span class="delivery_warning">' . $row->delivery_date . '</span>';
}
else
{
    else { echo $row->delivery_date; };
}