hey ppl i got a problem! im trying to loop threw the last 12 months of this year from now and return the start of the current month in t he loop and the end of the current month in the loop if that makes send eg $months = array("last_month" => array($start_of_month => $end_of_month), "month_before_last" => array($start_of_month => $end_of_month) ); PHP: the reason being is i have several records in my database with the UNIX TIMESTAMP / time() and want to get the count(id) for each month im trying to create a graph to show submissions for that month. hope someone can help me as im really rubbish at maths! Thanks in advanced!
You can do it by using mktime(). Just get the year and month from the timestamp and leave out the rest in mktime and it will give you the timestamp for the starting second of the month. To get the end second you can use strtotime() like this: $end_of_month = strtotime( '+1 month', $start_of_month ) - 1; [code] That should give you the last second of the month you are working on. I haven't tested any of this though. But it should work. And you should get around all those pesty months with more or less days than the rest of the months. Code (markup):
ok i think this could work but but theres another issue! take a look and see $months = array(); $first_month = mktime(0, 0, 0, $i, 0, date("Y")); for($i=0;$i <= 12;$i++){ $first_month = mktime(0, 0, 0, $i, 0, date("y")); $end_of_month = strtotime('+1 month', $first_month) - 1; $months[$first_month] = $end_of_month; } foreach($months as $start => $end){ echo date("d-m-y",$start) . " ==> " . date("d-m-y",$end)."<br />"; } PHP: ###########OUTPUT 30-11-08 ==> 29-12-08 31-12-08 ==> 30-01-09 31-01-09 ==> 02-03-09 28-02-09 ==> 27-03-09 31-03-09 ==> 30-04-09 30-04-09 ==> 29-05-09 31-05-09 ==> 30-06-09 30-06-09 ==> 29-07-09 31-07-09 ==> 30-08-09 31-08-09 ==> 30-09-09 30-09-09 ==> 29-10-09 31-10-09 ==> 30-11-09 30-11-09 ==> 29-12-09 Code (markup): its looping the months close enought but its ++ the year when with the date("Y") any ideas
<?php $i = 0 and $times = array(); while ( ++$i <= 12 ) $times[$i] = strtotime(date('\1\s\t F Y ', strtotime( '-' . $i . ' months' ))); /* Show it worked */ foreach($times as $time) echo date('jS F Y', $time) . ' - ' . $time . "<br />\n"; PHP:
There is SQL that can do this. You can use GROUP BY MONTH(field) WHERE MONTH(field) = Month# Might want to check out the mysql date/time functions.