Papa De Beau Papa De Beau - 6 months ago 39
PHP Question

PHP check if date between two dates

I got this code from Stackoverflow and changed it slightly to work with today's date.

I want to check if today fits between two dates. But this is not working.
What am I missing?

$paymentDate = date('d/m/Y');
echo $paymentDate; // echos today!
$contractDateBegin = date('d/m/Y', '01/01/2001');
$contractDateEnd = date('d/m/Y', '01/01/2015');

if ($paymentDate > $contractDateBegin && $paymentDate < $contractDateEnd)
{
echo "is between";
}
else
{
echo "NO GO!";
}

Answer

This is the right answer for your code.. Just use strtotime() php function

    $paymentDate = date('Y-m-d');
    $paymentDate=date('Y-m-d', strtotime($paymentDate));;
    //echo $paymentDate; // echos today! 
    $contractDateBegin = date('Y-m-d', strtotime("01/01/2001"));
    $contractDateEnd = date('Y-m-d', strtotime("01/01/2012"));

    if (($paymentDate > $contractDateBegin) && ($paymentDate < $contractDateEnd))
    {
      echo "is between";
    }
    else
    {
      echo "NO GO!";  
    }
Comments