1

I have an array structured like this:

Array ( [0] => 24-12-2013 [1] => 25-12-2013 [2] => 26-12-2014 [3] => 27-12-2013 [4])

I would like to check if any of the dates in the array are within a given date range.

The date range is structured like this:

$start = (date("d-m-Y", strtotime('25-12-2013')));
$end =   (date("d-m-Y", strtotime('26'12'2013')));

I would like to know which dates in the array are within the date range.

2
  • Show us what you've tried Commented Nov 13, 2013 at 15:52
  • Why are you using strings to define $start and $end? Commented Nov 13, 2013 at 15:53

6 Answers 6

9

Couple things:

  • Use timestamps or DateTime objects to compare dates, not strings
  • Use date format YYYY-MM-DD to avoid potential ambiguity about your date format (d/m/y or m/d/y)

This code will do what you want:

$dates = array("2013-12-24","2013-12-25","2014-12-24","2013-12-27");
$start = strtotime('2013-12-25');
$end =   strtotime('2013-12-26');

foreach($dates AS $date) {
    $timestamp = strtotime($date);
    if($timestamp >= $start && $timestamp <= $end) {
        echo "The date $date is within our date range\n";
    } else {
        echo "The date $date is NOT within our date range\n";
    }
}

See it in action:

http://3v4l.org/GWJI2

Sign up to request clarification or add additional context in comments.

6 Comments

Your dates are not in the same format as theirs. The concept is correct but it won't work for them without some conversions.
@JohnConde Yeah I know. I point out that their current date format is ambiguous, and not a good idea.
PHP docs say that strtotime() will parse dates with European format if a dash is used (slash forces US format). So I created a test with the original date formats: 3v4l.org/v8YCY. It appears to be good in all but PHP 5.1.0-5.1.2.
if i change $end to strtotime('2013-12-27'); it doesnt detect that date is within the range?
@danyo Sure it does, see here: 3v4l.org/5kper (look at output for 5.1.3 - 5.5.5)
|
3
$dates = array ('24-12-2013', '25-12-2013', '26-12-2014', '27-12-2013');

$start = strtotime('25-12-2013');
$end =   strtotime('26-12-2013');

$inDateRange = count(
    array_filter(
        $dates,
        function($value) use($start, $end) {
            $value = strtotime($value);
            return ($value >= $start && $value <= $end); 
        }
    )
);

4 Comments

Interesting, I like it. Looks like PHP 5.3+ only?
I'd hope that only a minority of people are still running PHP < 5.3 by now
if i change $end to strtotime('27-12-2013'); it doesnt detect that date is within thee range?
Are you sure it doesn't? It's returning count of 2 for me if $end = strtotime('27-12-2013');
2
<?php
$start   = DateTime::createFromFormat('d-m-Y', '25-12-2013');
$end     = DateTime::createFromFormat('d-m-Y', '26-12-2013');
$dates   = array('24-12-2013','25-12-2013','26-12-2014','27-12-2013');
$matches = array();
foreach ($dates as $date) {
    $date2 = DateTime::createFromFormat('d-m-Y', $date);
    if ($date2 >= $start && $date2 =< $end) {
        $matches[] = $date;
    }
}
print_r($matches);

See it in action

2 Comments

Comparison should probably be $date2 >= $start && $date2 <= $end ?
I had the same thought and just came back to do it.
0
$_between = array();
$start = date('Ymd', strtotime($start));
$end = date('Ymd', strtotime($end));

foreach ($dates as $date)
{
   $date = date('Ymd',strtotime($date));
   if ($date > $start && $date < $end) {
       array_push($_between,$date);
       continue;
   }
}
echo '<pre>';
var_dump($_between);
echo '</pre>';

Comments

0

Loop over the array turning each date into unix time (seconds since Jan 1, 1970), and do simple math to see if the number of seconds is between the range. Like so:

$start = strtotime('25-12-2013');
$end =   strtotime('26'12'2013');

foreach($date in $dates) {
    $unix_time = strtotime($date);
    if($unix_time > $start && $unix_time < $end)
        //in range
 }

Comments

0
// PHP >= 5.3:

$dates_in_range = array_filter($array, function($date) {
    global $start;
    global $end;
    return (strtotime($date) >= strtotime($start) and strtotime($date) <= strtotime($end));
});

3 Comments

Why do you have to use global?
@MarkBaker global is the clasic way to register a variable inside a function in PHP, the only way until PHP 5.3. Your answer is smarter in this point.
When you're using closures anyway it's better to import them inside with use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.