How to determine if a date exists between two dates in PHP?

I need to know if $paymentDate(31/12/2010) exists between $contractDateBegin(01/01/2001) and $contractDateEnd(01/01/2012)

dd / mm / yyyy FORMAT!

+3
source share
3 answers

Like PHP 5.3:

$paymentDate = DateTime::createFromFormat('d/m/Y', '31/12/2010');
$contractDateBegin = DateTime::createFromFormat('d/m/Y', '01/01/2001');
$contractDateEnd = DateTime::createFromFormat('d/m/Y', '01/01/2012');

if ($paymentDate >= $contractDateBegin && $paymentDate <= $contractDateEnd)
{
  echo "is between\n";
}

You may need to tune the usage <=to <depending on whether the dates are exclusive.

+30
source

if they are formatted as YYYYMMDD, you can check if there are any $paymentDate > $contractDateBegin and $paymentDate < $contractDateEnd

This works with any number format that has larger formats. If you have American dates, such as MM / DD / YYYY, this will not work.

+3
source
$test = strtotime($paymentDate);
if ($test >= strtotime($contractDateBegin) && $test <= strtotime($contractDateEnd))
+3
source

All Articles