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
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 />";

}

?>

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.