Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-筛选数组中的日期键_Php_Arrays - Fatal编程技术网

PHP-筛选数组中的日期键

PHP-筛选数组中的日期键,php,arrays,Php,Arrays,我在尝试根据数组中的日期筛选结果时遇到了一段时间的问题。例如,我想创建一个用户指定年数的发票系统来搜索发票。因此,如果我指定3年,它只能收回3年的发票 我的发票数组如下所示: Array ( [Invoice_Array] => Array ( [0] => Array ( [0] => 2884 [1] => 1

我在尝试根据数组中的日期筛选结果时遇到了一段时间的问题。例如,我想创建一个用户指定年数的发票系统来搜索发票。因此,如果我指定3年,它只能收回3年的发票

我的发票数组如下所示:

Array
(
    [Invoice_Array] => Array
        (
            [0] => Array
                (
                    [0] => 2884
                    [1] => 15/08/2013
                    [2] => C
                    [3] => -61.54
                    [4] => 0.00
                )

            [1] => Array
                (
                    [0] => 2719
                    [1] => 13/06/2012
                    [2] => C
                    [3] => -200.00
                    [4] => 0.00
                )

            [2] => Array
                (
                    [0] => 99900
                    [1] => 02/03/2011
                    [2] => I
                    [3] => 50.00
                    [4] => 0.00
                )

            [3] => Array
                (
                    [0] => 5685
                    [1] => 02/03/2011
                    [2] => C
                    [3] => -50.00
                    [4] => 0.00
                )

        )

    [Invoice_Count] => 4
)
我试着使用另一个问题的答案:

foreach ($myarray as $item)
    if ($item->dateitem >= $startDate  &&  
        $item->dateitem <= $endDate)
            $newarray[] = $item;
foreach($myarray作为$item)
如果($item->dateitem>=$startDate&&

$item->dateitem首先,您的日期由
dd/mm/YYYY
表示,因此不是以strotime()可以识别的格式

$newarray = array();
$boundary = strotime('today minus '.$years.' year');
foreach ($myarray as $item) {
    $parts = explode('/', $item[1]);
    $date = sprintf('%04d-%02d-%0d', $parts[2], $parts[1], $parts[0]);
    if ($strtotime($date)>$boundary)
            $newarray[] = $item;
}
关注“三年前”(2012年8月18日)和“过去三年”(2012-2014年)之间的差异

例如,如果您想追溯到$years年前:


在新数组中收集
$myarray
的选定条目
$newarray

这里的
$startDate
$endDate
是什么,这是一个问题。我很遗憾地不确定。如何从用户那里获得
3年
的值?@PrerakSola作为“3”的整数,他们在WordPress控制面板的插件中键入它。因此,我们可以假设它的
$years=3;
为例,我明白你所说的“三年前”是什么意思了,很好,它应该是“今天减去$years”。非常感谢您的帮助,我将玩一玩它并返回给您。使用我添加到问题中的代码使它工作。非常感谢!!
$parts = explode('/', $date);
$date = sprintf('%04d-%02d-%0d', $parts[2], $parts[1], $parts[0]);
$newarray = array();
$boundary = strotime('today minus '.$years.' year');
foreach ($myarray as $item) {
    $parts = explode('/', $item[1]);
    $date = sprintf('%04d-%02d-%0d', $parts[2], $parts[1], $parts[0]);
    if ($strtotime($date)>$boundary)
            $newarray[] = $item;
}