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? :)

 

 

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.