fohanlon Posted November 22, 2007 Share Posted November 22, 2007 Hi Guys, I know you can get the current month using data("m"); How can I output the last 6 months (numeric value "m" and text representation "M") from the current month including the current month. Have tried a few solutions but falling down when current month is for example 2 it should print out 2- Feb 1 - Jan 12 - Dec 11 - Nov 10 - Oct 9 - Sept Any ideas/help would be greatly appreciated. Many thanks, Fergal. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 22, 2007 Share Posted November 22, 2007 strtotime() is your friend: <?php for($i = 0;$i<6;$i++){ $time = strtotime("today - $i months"); echo date("m M",$time).'<br />'; } ?> Quote Link to comment Share on other sites More sharing options...
fohanlon Posted November 22, 2007 Author Share Posted November 22, 2007 Ben Thanks for this code. Seems to be nearly there. When I run it it prints: 11 Nov 12 Dec 01 Jan 02 Feb 03 Mar 04 Apr However I need it to print: 11 Nov 10 Oct 9 Sept 8 Aug 7 July 6 Jun What would I change to sort this out Thanks, Fergal. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 22, 2007 Share Posted November 22, 2007 The code i posted works fine for me. Are you sure you copied it exactly? You didn't change a minus to a plus by accident? Quote Link to comment Share on other sites More sharing options...
fohanlon Posted November 22, 2007 Author Share Posted November 22, 2007 Hi Ben Solved it as follows: for($i = 0; $i<6; $i++) { $time = strtotime("-$i months"); echo date("m M", $time).'<br />'; } Many thanks for your help. Hopefully this should work for all months. strtotime works from current timestamp if none is supplied. Regards Fergal. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 22, 2007 Share Posted November 22, 2007 Very strange. Out of interest, what version of PHP are you running? I assume the issue lies somewhere with 'today'. It works fine for me locally under 5.1.6 and fine on a host which is running 5.2.4. Quote Link to comment Share on other sites More sharing options...
fohanlon Posted November 23, 2007 Author Share Posted November 23, 2007 Yes agreed looks like today is the problem alright. Have PHP 5.1.2 on my local machine and your code is perfect. Version 4.something on hosting solution - issue must be here. Thanks, Fergal. 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.