-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?

6
  • Are you using DateTime objects or just strings? Commented Jan 29, 2013 at 9:48
  • I am using timestamps created by using the strtotime() function. Commented Jan 29, 2013 at 9:49
  • With an if and a few comparisons? What exactly is it that you have a problem with? Commented Jan 29, 2013 at 9: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? Commented Jan 29, 2013 at 9:54
  • Compare the dates! It doesn't get more basic than that. Commented Jan 29, 2013 at 9:57

1 Answer 1

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';
}
Sign up to request clarification or add additional context in comments.

1 Comment

The OP has a date with duration that needs to be checked.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.