1

I am trying to filter an array to output all the children who will be on holidays within a date range (i.e 14-11-2016 till 18-11-2016) in PHP.

I get the following data in an array and I do not have any control on creation of the data.

[{"holiday":"15-11-2016","name":"Josh Stevens"},{"holiday":"17-11-2016","name":"Josh Stevens"},{"holiday":"22-11-2016","name":"Josh Stevens"},{"holiday":"14-11-2016","name":"Naomi Christ"},{"holiday":"15-11-2016","name":"Naomi Christ"},{"holiday":"16-11-2016","name":"Naomi Christ"},{"holiday":"17-11-2016","name":"Naomi Christ"},{"holiday":"14-11-2016","name":"Jasmine Auger"},{"holiday":"15-11-2016","name":"Jasmine Auger"},{"holiday":"16-11-2016","name":"Jasmine Auger"},{"holiday":"17-11-2016","name":"Jasmine Auger"}]

I need to output data for only the ones whose "holiday" are >= 14-11-2016 and <= 18-11-2016.

I have tried the following logic but it does not return anything

function filterArray($value)
{
  return ($value > '14-11-2016');
}

$filteredArray = array_filter($fullArray, 'filterArray');

foreach($filteredArray as $k => $v)
{
 echo "$k = $v";
}
1
  • Use the DateTime class to parse the date. Afterwards compare the DateTime object you previously created so that it belongs on the date range you mentioned. Commented Nov 13, 2016 at 14:16

2 Answers 2

3

If your are sure that 'holiday' field contains correct data, then you can use this code:

$fullArray = json_decode( '[{"holiday":"15-11-2016","name":"Josh Stevens"},{"holiday":"17-11-2016","name":"Josh Stevens"},{"holiday":"22-11-2016","name":"Josh Stevens"},{"holiday":"14-11-2016","name":"Naomi Christ"},{"holiday":"15-11-2016","name":"Naomi Christ"},{"holiday":"16-11-2016","name":"Naomi Christ"},{"holiday":"17-11-2016","name":"Naomi Christ"},{"holiday":"14-11-2016","name":"Jasmine Auger"},{"holiday":"15-11-2016","name":"Jasmine Auger"},{"holiday":"16-11-2016","name":"Jasmine Auger"},{"holiday":"17-11-2016","name":"Jasmine Auger"}]', true );


//I need to output data for only the ones whose "holiday" are >= 14-11-2016 and <= 18-11-2016.


function filterArray($value)
{
        static $from  = 20161114;
        static $until = 20161118;

        // parse the date and convert it to an int.    
        $v = new \DateTime( $value['holiday'] );
        $v = (int)$v->format('Ymd');

        // compare as integer
        return ($v >= $from && $v <= $until );
}

$filteredArray = array_filter($fullArray, 'filterArray');

foreach($filteredArray as $k => $v)
{
        var_dump($v); // prints sub-array
}

You can also compare DateTime objects:

//I need to output data for only the ones whose "holiday" are >= 14-11-2016 and <= 18-11-2016.

$from  = new \DateTime('2016-11-14');
$until = new \DateTime('2016-11-18');

function filterArray($value)
{
        global $from, $until;
        $v = new \DateTime( $value['holiday'] );
        return ($v >= $from && $v <= $until );
}
Sign up to request clarification or add additional context in comments.

Comments

1
function filterArray($value)
{
  return (strtotime('18-11-2016') >= strtotime($value["holiday"])) && (strtotime($value["holiday"]) >= strtotime('14-11-2016'));
}
$string = '[{"holiday":"15-11-2016","name":"Josh Stevens"},{"holiday":"17-11-2016","name":"Josh Stevens"},{"holiday":"22-11-2016","name":"Josh Stevens"},{"holiday":"14-11-2016","name":"Naomi Christ"},{"holiday":"15-11-2016","name":"Naomi Christ"},{"holiday":"16-11-2016","name":"Naomi Christ"},{"holiday":"17-11-2016","name":"Naomi Christ"},{"holiday":"14-11-2016","name":"Jasmine Auger"},{"holiday":"15-11-2016","name":"Jasmine Auger"},{"holiday":"16-11-2016","name":"Jasmine Auger"},{"holiday":"17-11-2016","name":"Jasmine Auger"}]';

$fullArray = json_decode($string, true);
$filteredArray = array_filter($fullArray, 'filterArray');

foreach($filteredArray as $k => $v)
{
 echo "$k = ". $v['holiday'] ." - ".$v['name']."<br/>";
}

And using closures from PHP 5.3 and upper versions:

$from = strtotime('14-11-2016');
$to = strtotime('18-11-2016');
$string = '[{"holiday":"15-11-2016","name":"Josh Stevens"},{"holiday":"17-11-2016","name":"Josh Stevens"},{"holiday":"22-11-2016","name":"Josh Stevens"},{"holiday":"14-11-2016","name":"Naomi Christ"},{"holiday":"15-11-2016","name":"Naomi Christ"},{"holiday":"16-11-2016","name":"Naomi Christ"},{"holiday":"17-11-2016","name":"Naomi Christ"},{"holiday":"14-11-2016","name":"Jasmine Auger"},{"holiday":"15-11-2016","name":"Jasmine Auger"},{"holiday":"16-11-2016","name":"Jasmine Auger"},{"holiday":"17-11-2016","name":"Jasmine Auger"}]';

$fullArray = json_decode($string, true);
$filteredArray = array_filter($fullArray, function($elem) use ($from, $to){
    $holidayTime = strtotime($elem['holiday']);
    return ($from <= $holidayTime) && ($holidayTime <= $to);
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.