PHP and MySQL - Comparing dates near maturity

297 Views Asked by At

I'm trying to do a system that compare an specific date ($date) with the atual date (date('Y/m/d')), but I need to color this $date when he is one week to the atual date (near the maturity date). I don't know if you can understand me, my english is bad...

Thank you for reading this.

2

There are 2 best solutions below

2
On

Something like this may work:

date_default_timezone_set('America/Los_Angeles');
$date = DateTime::createFromFormat('m-d-Y', '04-15-2013');
$maturityDate = DateTime::createFromFormat('m-d-Y', '04-20-2013');
$maturityDateMinus10Days = DateTime::createFromFormat('m-d-Y', '04-10-2013');

if ($date > $maturityDateMinus10Days
    && $date < $maturityDate) {
    echo 'date is within 10 days of maturity 10';
}

You can test it by copying and pasting here http://writecodeonline.com/php/

Ref. PHP - add 1 day to date format mm-dd-yyyy

Ref. How can I check if the current date/time is past a set date/time?

4
On

Do it in the MySQL query:

SELECT maturity_date,
       maturity_date < DATE_ADD(NOW(), INTERVAL 1 WEEK) AS near_maturity,
       ...

Then your PHP code can use if ($row['near_maturity') to color the date.