php - Why is my while loop not working when no echo is put inside? -


i got pretty simple code take 2 dates , loop data until end date reached.

            $start = new datetime($senddate);             $now = new datetime("now");             $end = new datetime ($end);               //we check if starting date >= now()             if ($start->date <= $now->date){                 $start = $now;              }              $i=0;              if ($frequency==4){                 while ($start->date <= $end->date) {                     $calcdate[$i]=$start->date;                     $start->modify('+1 month');                     $i++;                     echo '<!--';                     print_r($start);                     echo '-->';                 } 

as see there print_r inside loop. work fine :) but, if remove it, loop never end .. tried add if($i>50) exit; without anymore success. don't understand why loop doesn't work when no pint_r inside.

thanks help

i suggest have read of php datetime manual, there lot of information there trying do.

as far can tell, trying carry out operation on monthly basis between 2 dates span current date. there simpler way of doing utilising dateperiod class.

something this:-

$start = new \datetime('yesterday');// demo purposes $now = new \datetime(); //no need "now" default $end = new \datetime('+ 6 month');// again, demo purposes $interval = new \dateinterval('p1m');  if($now >= $start){ // can direct comparisons on datetime objects     $period = new \dateperiod($start, $interval, $end);     foreach($period $date){         // each $date datetime instance can         // operate on see fit, have var_dumped out.         var_dump($date);     } } 

the above code can seen working here http://3v4l.org/1i30e


Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -