Counting number of elements in array on a given date

Hi,

I’m having some trouble trying to figure how to go about this issue so hopefully you guys can give me a helping hand :slight_smile:

I have an array which contains numerous date elements.

How would I go about to be able to count number of elements which is todays date - 1 day, todays date -2 days, todays date -3 days .etc etc…

I need to know how many elements there are with a given day inside this array. Anyone? :slight_smile:

Hi there,

As I don’t know how the original array is storing its dates, I’ve written a short snippet presuming its word format or 02-03-2011 (something other than timestamp).
[php]
$myarray = array(); //Original array (yours will be full of dates)
$dategroups = array();
foreach($myarray as $date)
{
$dategroups[$date] = (isset($dategroups[$date])) ? $dategroups[$date]+1 : 1;
}
[/php]

This will fill the array $dategroups with a key for each date, the value being how many occurences it found.

If there are some dates that are the same but in a different format (e.g. 02/03/2011 and 2nd March 2011), you may want to replace $dategroups[$date] with $dategroups[strtotime($date)] - but bear in mind that the keys of the elements in the array will be unix timestamps (e.g. 1300474233) rather than the legible formats written above (use date(“d-m-Y”,$timestamp) to convert back).

Let me know how you get on and whether or not this was of any use to you!

Sponsor our Newsletter | Privacy Policy | Terms of Service