celsowm
celsowm

Reputation: 404

How can I determine if a date is between two dates in PHP?

I need know if a $paymentDate (31/12/2010) is valid between $contractDateBegin(01/01/2001) and $contractDateEnd(01/01/2012)

dd/mm/yyyy FORMAT !

Upvotes: 3

Views: 28822

Answers (3)

Matthew
Matthew

Reputation: 48284

As of 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 adjust the use of <= to < depending on whether or not the dates are exclusive.

Upvotes: 30

patapizza
patapizza

Reputation: 2398

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

Upvotes: 3

jberg
jberg

Reputation: 4818

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

This works with any numeric format that has the larger formats first. If you have american dates for example MM/DD/YYYY, it doesn't work.

Upvotes: 3

Related Questions