-1

Possible Duplicate:
PHP, see if date range is partly within another date range

I have 2 dates, a start date (2011-01-01) and an end date (2011-02-28).

I have a third date that starts on 2010-01-01 and ends on 2012-03-01. This means that this third date falls within the first 2 dates range.

If the third dates start and/or end date is not within the 2 dates then it must be false.

How can I check this using php?

Community
  • 1
  • 1
Wesley
  • 87
  • 2
  • 5
  • Are you using DateTime objects or just strings? –  Jan 29 '13 at 09:48
  • I am using timestamps created by using the strtotime() function. – Wesley Jan 29 '13 at 09:49
  • With an `if` and a few comparisons? What exactly is it that you have a problem with? – Jon Jan 29 '13 at 09:53
  • A scenario would be: A project starts on 2011-01-01 and ends on 2011-10-01. I would love to calculate the revenue for 2011-05-01 to 2011-05-31. How can I tell that the project falls within the period? – Wesley Jan 29 '13 at 09:54
  • Compare the dates! It doesn't get more basic than that. – Jon Jan 29 '13 at 09:57
  • Your "third date" is not a date. A date doesn't start on a one date, and ends on another date. – Nanne Jan 29 '13 at 10:01

1 Answers1

0

You can do the following:

$startDate = strtotime('2011-01-01');
$endDate = strtotime('2011-02-28');

$intervalStart = strtotime('2011-02-01');
$intervalEnd = strtotime('2012-03-01');


if ($startDate < $intervalStart || $endDate > $intervalEnd) {
    echo 'Not in the interval';
} else {
    echo 'In the interval';
}
Vlad Preda
  • 9,780
  • 7
  • 36
  • 63