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 Link to comment https://forums.phpfreaks.com/topic/50337-list-all-months-since-yyyy-mm-dd/ 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 Link to comment https://forums.phpfreaks.com/topic/50337-list-all-months-since-yyyy-mm-dd/#findComment-247146 Share on other sites More sharing options...
Canman2005 Posted May 7, 2007 Author Share Posted May 7, 2007 Sweet Link to comment https://forums.phpfreaks.com/topic/50337-list-all-months-since-yyyy-mm-dd/#findComment-247147 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 Link to comment https://forums.phpfreaks.com/topic/50337-list-all-months-since-yyyy-mm-dd/#findComment-247154 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 ?> Link to comment https://forums.phpfreaks.com/topic/50337-list-all-months-since-yyyy-mm-dd/#findComment-247157 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 ?> Link to comment https://forums.phpfreaks.com/topic/50337-list-all-months-since-yyyy-mm-dd/#findComment-247158 Share on other sites More sharing options...
Canman2005 Posted May 7, 2007 Author Share Posted May 7, 2007 Thanks Link to comment https://forums.phpfreaks.com/topic/50337-list-all-months-since-yyyy-mm-dd/#findComment-247191 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.