Flag for United States of America asked on

php year month loop function

$curYear = date('Y');
          for ($k = $curYear; $k >= 2011; $k--)

          for ($i = 12; $i >= 1; $i--)

$monthName = date("F", mktime(0, 0, 0, $i, 10));

Open in new window

what i want to modify in this code is

like if year = 2012 i will start from 2012 and the current month ...and decrement the month till January..

and if year less than curr year like 2011 fetch all month..

let say we are in year 2013 so the output is
2013 from month 12 to 1
and 2012 from month 12 to 1
and 2011 from month 12 to 1

i want to modify it to be like
2013 from current  month  to 1
and 2012 from month 12 to 1
and 2011 from month 12 to 1
PHPWeb Languages and StandardsWeb Development

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon

well to get months this year to today:

$thisYear = range(1,date("n"));
foreach ($thisYear as $month){
print date("F",mktime(0,0,0,$month,10));

//previous years :

$thisYear = range(1,12); //then just add years

Open in new window

Ray Paseur

Please read the article here.  It will tell you everything you need to know about the basics of handling PHP and SQL date/time values.
Ray Paseur

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.