svenn Posted March 18, 2011 Share Posted March 18, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/231017-need-to-count-number-of-elements-with-given-date-value-in-array/ Share on other sites More sharing options...
Pikachu2000 Posted March 18, 2011 Share Posted March 18, 2011 Is it just the date by itself in the element, or is it part of a larger string? Quote Link to comment https://forums.phpfreaks.com/topic/231017-need-to-count-number-of-elements-with-given-date-value-in-array/#findComment-1189241 Share on other sites More sharing options...
svenn Posted March 18, 2011 Author Share Posted March 18, 2011 Hi, thanks for the quick reply At this point each element contains only a date : [yyyy-mm-dd] Quote Link to comment https://forums.phpfreaks.com/topic/231017-need-to-count-number-of-elements-with-given-date-value-in-array/#findComment-1189247 Share on other sites More sharing options...
Pikachu2000 Posted March 18, 2011 Share Posted March 18, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/231017-need-to-count-number-of-elements-with-given-date-value-in-array/#findComment-1189253 Share on other sites More sharing options...
svenn Posted March 19, 2011 Author Share Posted March 19, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/231017-need-to-count-number-of-elements-with-given-date-value-in-array/#findComment-1189425 Share on other sites More sharing options...
sasa Posted March 19, 2011 Share Posted March 19, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/231017-need-to-count-number-of-elements-with-given-date-value-in-array/#findComment-1189429 Share on other sites More sharing options...
svenn Posted March 19, 2011 Author Share Posted March 19, 2011 Perfect! Thank you SO much for your time and help Quote Link to comment https://forums.phpfreaks.com/topic/231017-need-to-count-number-of-elements-with-given-date-value-in-array/#findComment-1189538 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.