Trying to build array from date range

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
accident
Forum Newbie
Posts: 5
Joined: Tue Jul 25, 2006 10:08 pm

Trying to build array from date range

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Post Reply