Jump to content

Need to count number of elements with given date value in array


svenn

Recommended Posts

Hi guys :)

 

I'm kinda stuck..so hopefully you guys can lend me a hand :)

 

I've got an array containing date elements ("Y-m-d")...

 

I'm trying to output some data into googlecharts..so i need to count how many elements that are in the array with todays date -1 day, todays date-2 days...todays date -3days etc... Up until a set number of days (for example 7 days, 14 days etc)..

 

Any advice on how to go about to achieve this? :)

 

 

If it's going to stay that way, this should work. If it changes and there is more than just the date in the element, you'll probably need to use a regex pattern in a loop. The following code assumes a single dimensional array . . .

 

$array = // your array
$array = array_map('trim', $array);
$values = array_count_values($array);
$today = date('Y-m-d');
$total = $values[$today];
echo $total;

Hi :)

 

Ok, seems I was wrong about the array contents.. it contains the following:

Array ( [0] => stdClass Object ( [date_time] => 2011-03-16 08:36:11 ) 
[1] => stdClass Object ( [date_time] => 2011-03-17 04:29:05 )

 

 

Bleh....then perhaps I need to go about this issue in another matter? Tried the following:

 

foreach($result as $results):
				$datoformat = date("Y-m-d", strtotime($results->date_time));
				$datoformat = array_map('trim', $datoformat);
				$values = array_count_values($datoformat);
				$yesterday = date('Y-m-d', strtotime("-1 days"));
				$total = $values[$yesterday];

					echo $total;

 

But this yields an error:

Warning: array_map() [function.array-map]: Argument #2 should be an array...
Warning: array_count_values() expects parameter 1 to be array, null given...

 

change to

$datoformat = array();
foreach($result as $results) $datoformat[] = date("Y-m-d", strtotime($results->date_time));
$datoformat = array_map('trim', $datoformat);
$values = array_count_values($datoformat);
$yesterday = date('Y-m-d', strtotime("-1 days"));
$total = $values[$yesterday];
echo $total;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.