0

I have an array of date strings:

Array
(
    [0] => 2014-03-24
    [1] => 2014-03-18
    [2] => 2014-06-08
    [3] => 2014-08-21
    [4] => 2014-09-11
)

I'd like to use the array_filter function to filter out all the entries older than a specific month and year:

array_filter($array, "newer_than");

function newer_than($var)
{
    return (strtotime($var) > CURRENT MONTH AND YEAR);
}

Any help would be appreciated

Thanks

1
  • 1
    You want to filter out entries before the current time? Use time() in place of CURRENT MONTH AND YEAR. Commented Jul 13, 2014 at 17:35

2 Answers 2

7

strtotime() is smart enough to get the UNIX timestamp of the current month:

$thisMonth = strtotime('first day of ' . date('F Y'));

$array2 = array_filter($array1, function ($val) use ($thisMonth) {
    return strtotime($val) > $thisMonth;
});
Sign up to request clarification or add additional context in comments.

Comments

2

If you want to specify a month and year that are not the current ones, you can do something like this:

    <?
    $array[] = "2014-03-24";
    $array[] = "2014-03-18";
    $array[] = "2014-06-08";
    $array[] = "2014-08-21";
    $array[] = "2014-09-11";

    $array = array_filter($array, "newer_than");

    function newer_than($var)
    {
            $specificMonth = 8;
            $specificYear = 2014;

            return (strtotime($var) > mktime(0,0,0,$specificMonth,1,$specificYear) );
    }

    ?>

Comments

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.