Link to home
Start Free TrialLog in
Avatar of Member_2_6478753
Member_2_6478753Flag 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
Avatar of darren-w-
darren-w-
Flag of United Kingdom of Great Britain and Northern Ireland image

well to get months this year to today:

<?php 
$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

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.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial