date - php - for loop for each month of year -


i want loop checks current month, 12 months in future , 4 months in past.

for example: today 1st august 08. loop should go through april, may, june, july, august, september, october, november, december, january, february, march, april, may, june, july, , august.

i have tried strotime don't know how can loop 4 months , 12 months in future.

here code

$i = 1;  $month = strtotime('2013-08-01');      while($i <= 12) {         $month_name = date('f', $month);         echo $month_name;         echo "<br>";          $month = strtotime('+1 month', $month);         $i++; 

i think yoshi there his answer, using dateperiod datetime more consistent , makes more readable code imho:-

$onemonth = new \dateinterval('p1m'); $startdate = \datetime::createfromformat('d h:i:s', '1 00:00:00')->sub(new \dateinterval('p4m')); $period = new \dateperiod($startdate, $onemonth, 16);  foreach($period $date){     //$date instance of \datetime. i'm var_dumping illustration     var_dump($date); } 

see working


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -