Page 1 of 1

Trying to build array from date range

Posted: Fri Aug 04, 2006 12:10 am
by accident
Hi all, My brain must be dead today, because this relatively simple solution I am trying to come up with, I just can't get it working.

I have a class like this

Code: Select all

class item
{
      private $itemName;
      private $activeFrom;
      private $activeTo;

function __construct($itemName, $activeFrom, $activeTo)
{
   $this->itemName = $itemName;
   $this->activeFrom = $activeFrom;
   $this->activeTo = $activeTo;			
}
}

Now I have an array of class item, this is the array

Code: Select all

$itemArray = array();

$item = new item("item1", "2006-01-01", "2006-01-10");

$itemArray[] = $item;

$item = new item("item2", "2006-01-10", "2006-01-15");

$itemArray[] = $item;

$item = new item("item3", "2006-01-15", "2006-01-20");

$itemArray[] = $item;

$item = new item ("item4", "2006-01-20", "2006-01-28");

$itemArray[] = $item;
alright, now what I want to do is this ...

Code: Select all

$startDate = "2006-01-05";
$endDate = "2006-01-18";
I then want a loop that loops threw the $itemArray and builds a new array that is active between that date range.

So with that start and end date, item1, item2, and item3 should all be in the array, but item4 shouldnt since it doesnt fall in the range.

Now I know this should be relatively simple, but for some reason my brain isn't working today.

Thanks in advance

Posted: Fri Aug 04, 2006 1:51 pm
by RobertGonzalez
I'm not sure you can do what you want the way you are doing it. But, to try to prove me wrong, try this...

Code: Select all

$itemArray = array(
    new item("item1", "2006-01-01", "2006-01-10"),
    new item("item2", "2006-01-10", "2006-01-15"),
    new item("item3", "2006-01-15", "2006-01-20"),
    new item ("item4", "2006-01-20", "2006-01-28")
);
I have my doubts that this will work, and I am even more curious as to why you are filling an array with a bunch of objects.