0

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!

4
  • What is $startDate and $endDate over here Commented Aug 18, 2015 at 10:08
  • stackoverflow.com/a/10668264/4978704 it was this question. I'm not sure sadly. Commented Aug 18, 2015 at 10:09
  • How do you get the value 3 Years from the user? Commented Aug 18, 2015 at 10:15
  • @PrerakSola As an integer of "3", they type it into my plug-in at the WordPress control panel. So we can just assume its $years = 3; for sake of example Commented Aug 18, 2015 at 10:17

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Ah I see what you mean by "three years back", good point, it should indeed be 'today minus $years'. Thanks a lot for this help, I'll have a play with it and get back to you.
Got it working with the code I've added to my question. Thanks a lot!!

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.