flyerball10 Posted June 29, 2007 Share Posted June 29, 2007 I have this code that count back in months for 1 year. For some reason it outputs MARCH twice. Any idea? $month = array ('now', 'now -1 months', 'now -2 months', 'now -3 months', 'now -4 months', 'now -5 months', 'now -6 months', 'now -7 months', 'now -8 months', 'now -9 months', 'now -10 months', 'now -11 months'); foreach ($month as $a) { $date=strtotime($a); $date=date("F Y", $date); echo $date.'<br>'; } /////Output June 2007 May 2007 April 2007 March 2007 March 2007 January 2007 December 2006 November 2006 October 2006 September 2006 August 2006 July 2006 Link to comment https://forums.phpfreaks.com/topic/57757-solved-date-array-problems/ Share on other sites More sharing options...
Wildbug Posted June 29, 2007 Share Posted June 29, 2007 It's skipping February, too. Probably because now (June 29th) - 4 months = February 29th, which didn't exist this year, so is converted to March 1st. Edit, possible solution: just use the first of the month. Link to comment https://forums.phpfreaks.com/topic/57757-solved-date-array-problems/#findComment-286069 Share on other sites More sharing options...
GingerRobot Posted June 29, 2007 Share Posted June 29, 2007 Here's what i came up with: <?php for($x=0;$x<12;$x++){ $curr_month = date("m"); $curr_year = date("Y"); $date = strtotime("$curr_month/01/$curr_year - $x months"); echo date("F Y",$date); echo '<br />'; } ?> Link to comment https://forums.phpfreaks.com/topic/57757-solved-date-array-problems/#findComment-286086 Share on other sites More sharing options...
flyerball10 Posted June 29, 2007 Author Share Posted June 29, 2007 That works great, except i need it to go back 1 year. the output is going forward 1 year ///Output June 2007 July 2007 August 2007 September 2007 October 2007 November 2007 December 2007 January 2008 February 2008 March 2008 April 2008 May 2008 Link to comment https://forums.phpfreaks.com/topic/57757-solved-date-array-problems/#findComment-286110 Share on other sites More sharing options...
GingerRobot Posted June 29, 2007 Share Posted June 29, 2007 Really? I get this: June 2007 May 2007 April 2007 March 2007 February 2007 January 2007 December 2006 November 2006 October 2006 September 2006 August 2006 July 2006 Are you sure you've used the exact same code? Link to comment https://forums.phpfreaks.com/topic/57757-solved-date-array-problems/#findComment-286123 Share on other sites More sharing options...
Wildbug Posted June 29, 2007 Share Posted June 29, 2007 Ditto. <?php $month = array ($first = date('Y-m-15'), "$first -1 months", "$first -2 months", "$first -3 months", "$first -4 months", "$first -5 months", "$first -6 months", "$first -7 months", "$first -8 months", "$first -9 months", "$first -10 months", "$first -11 months"); foreach ($month as $a) echo date("F Y\n", strtotime($a)); ?> Output June 2007 May 2007 April 2007 March 2007 February 2007 January 2007 December 2006 November 2006 October 2006 September 2006 August 2006 July 2006 Link to comment https://forums.phpfreaks.com/topic/57757-solved-date-array-problems/#findComment-286125 Share on other sites More sharing options...
flyerball10 Posted June 29, 2007 Author Share Posted June 29, 2007 Beautiful, thanks for the help everyone. Link to comment https://forums.phpfreaks.com/topic/57757-solved-date-array-problems/#findComment-286128 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.