Ridai
Ridai

Reputation: 179

PHP - Filtering date keys in array

I've been having trouble for a while now trying to filter my results based on the dates in the array. For example, I want to create an invoice system with a user specified amount of years to search for invoices. So if I specified 3 years, it would only bring back 3 years of invoices.

My invoice array looks something like this:

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
)

I've tried using this from another questions answer:

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

But I'm having trouble implementing it for my array above. What would be a good way to go 'into' the array at the dates, and then filter the results so that all the other data is still present, except for the keys outside of the date range? (So if I only wanted 3 years of data back from now, only key [0] would be there).

I think the first thing to work out would be how to check the date in each key/invoice against the date range specified. Perhaps a foreach that loops through the invoices, and an if statement that checks if the current pointer position is in that date range somehow?

Finished:

$years = 3;
$myarray = array(
    'Invoice_Count' => 6,
    'Invoice_List' => array(
        array('31515', 'I', '255.60', '255.60', '15/06/2015'),
        array('31664', 'I', '330.00', '330.00', '22/07/2015'),
        array('31675', 'I', '4088.40', '4088.40', '27/07/2015'),
        array('31650', 'I', '245.70', '245.70', '29/07/2010')
        ),
    );

$newarray = array();
$boundary = strtotime('today -'.$years.' year');
foreach ($myarray['Invoice_List'] as $item) {
    $parts = explode('/', $item[4]);
    $date = sprintf('%04d-%02d-%0d', $parts[2], $parts[1], $parts[0]);
    if (strtotime($date)>$boundary)
            $newarray[] = $item;
}
print_r(strtotime($date)); echo " - "; print_r($boundary);
echo "<pre>"; print_r($newarray); echo "</pre>";

Thanks a lot to syck!

Upvotes: 0

Views: 119

Answers (1)

syck
syck

Reputation: 3029

First of all, your date is represented by dd/mm/YYYY and therefore not in a format recognized by strtotime(). So you should do something like

$parts = explode('/', $date);
$date = sprintf('%04d-%02d-%0d', $parts[2], $parts[1], $parts[0]);

Keep an eye on the difference between "three years back" (back to august 18th 2012) and "the last three years" (2012-2014).

As an example, if you want to go $years years back:

$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;
}

to collect the selected entries of $myarray in the new array $newarray.

Upvotes: 2

Related Questions