Canman2005 Posted May 7, 2007 Share Posted May 7, 2007 Hi all How can I print in a list format, all the dates since 2006-12-01 up to today, for for example December 2006 Janurary 2007 February 2007 March 2007 April 2007 and then as we get into a new month, it would be added onto the bottom. I'm sure ive asked this before but I cant seem to find the post or example I saved. Any help would be spot on Thanks Dave Quote Link to comment Share on other sites More sharing options...
paul2463 Posted May 7, 2007 Share Posted May 7, 2007 <?php $startdate = "1 december 2006"; function printMonths($var) { $start = strtotime($var); //timestamp of the entered date $now = strtotime("Now"); //timestamp of now so it does not write anything in the future while ($start < $now) //while the start is less than now { echo date("F Y", $start); //echo the month and year echo "<BR>"; //line break $start = strtotime("+1 month", $start); // add a month onto the start timestamp } } printMonths($startdate); //execute the function ?> prints out December 2006 January 2007 February 2007 March 2007 April 2007 May 2007 Quote Link to comment Share on other sites More sharing options...
Canman2005 Posted May 7, 2007 Author Share Posted May 7, 2007 Sweet Quote Link to comment Share on other sites More sharing options...
Canman2005 Posted May 7, 2007 Author Share Posted May 7, 2007 Can this be reversed to display the months in a reverse order? Newest at top Quote Link to comment Share on other sites More sharing options...
paul2463 Posted May 7, 2007 Share Posted May 7, 2007 <?php $startdate = "1 december 2006"; function printMonths($var) { $start = strtotime($var); $now = strtotime("Now"); while ($start < $now) { $dateArray[] = $start; //add the timestamps to an array $start = strtotime("+1 month", $start); } $revArray = array_reverse($dateArray); //reverse the array as they will be increasing in number for($i=0; $i <count($revArray); $i++) //iterate over the array { echo date("F Y", $revArray[$i]); //print out the dates echo "<BR>"; } } printMonths($startdate); //execute the function ?> Quote Link to comment Share on other sites More sharing options...
paul2463 Posted May 7, 2007 Share Posted May 7, 2007 or making it even easier <?php $startdate = "1 december 2006"; function printMonths($var) { $start = strtotime($var); $now = strtotime("Now"); while ($now > $start) { echo date("F Y", $now); echo "<BR>"; $now = strtotime("-1 month", $now); } } printMonths($startdate); //execute the function ?> Quote Link to comment Share on other sites More sharing options...
Canman2005 Posted May 7, 2007 Author Share Posted May 7, 2007 Thanks Quote Link to comment 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.