使用array_filter过滤日期字符串数组

lee*_*mon 0 php arrays date filter

我有一个日期字符串数组:

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

我想使用array_filter函数过滤掉特定月份和年份之前的所有条目:

array_filter($array, "newer_than");

function newer_than($var)
{
    return (strtotime($var) > CURRENT MONTH AND YEAR);
}
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激

谢谢

Chr*_*Lam 5

strtotime() 足够聪明,可以获得当月的UNIX时间戳:

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

$array2 = array_filter($array1, function ($val) use ($thisMonth) {
    return strtotime($val) > $thisMonth;
});
Run Code Online (Sandbox Code Playgroud)