Jump to content

Looping through dates


isedeasy

Recommended Posts

I am trying to get data out of my database to display in a graph, the problem I have is that if there are no rows on a certain date then that date does not get placed into the array.

 

I am thinking I will need to loop through previous dates and match the 'current working date' to the keys of my array to see the total number of rows

 

my array of data from the database will look like this :-

Array
(
    [2011 April 25th] => 32
    [2011 April 26th] => 70
    [2011 May 6th] => 86
    [2011 May 7th] => 86
    [2011 May 8th] => 87
    [2011 May 9th] => 86
    [2011 May 10th] => 86
    [2011 May 11th] => 79
)

 

notice the gap between april 26th and may 6th, thats because there are 0 rows in the database that fall on these days.

 

Whats the best way to loop through say the past month?

 

Hope this makes sense, if there is a better method I would be glad to hear it.

Link to comment
https://forums.phpfreaks.com/topic/236139-looping-through-dates/
Share on other sites

I'd do something like this

 

<?php

$date = array(4,2011);
$max = date('t',mktime(0,0,0,$date[0],1,$date[1]) );

$data = array(
4 => 'Some event on april 4th',
15 => 'Mid month maddddness',
23 => 'foobar',
51=> 'wut?'
);

for($d=1;$d<=$max;$d++) {

echo "$date[0]-$d has ";
if( isset($data[$d]) )
	echo "event {$data[$d]}";
else
	echo "no event";
echo "<br />";

}

?>

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.