Jump to content

[SOLVED] Print last 6 months


fohanlon

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/78475-solved-print-last-6-months/
Share on other sites

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.

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.