[Solved] While With Date Compare

hi everybody.

this seems really simple but it doesn’t work and I don’t know why … :(

here is my code. [edit : WORKING NOW]




//init

$dateTheorique = date('Y-m-d', strtotime("+$nbMois months", strtotime($dateTheorique)));

$i=0; //I added $i condition to avoid infinite loop


while(date('Y-m-d')>=date('Y-m-d',strtotime($dateTheorique))

{

     //test

     echo(date('Y-m-d').' > '.date('Y-m-d',strtotime($dateTheorique)).'</br>'); 

         //show 1st time: 2014-06-11 > 2014-12-01

         //show 2nd time: 2014-06-11 > 2015-03-01

         //show 3rd time: 2014-06-11 > 2015-06-01 ...

         // so the condition is not true but he keep looping until $i=5.

         // I also tried with strcmp() without any success... 


     //trt

     ...

     //end trt 


     //Incrementation

     $dateTheorique = date('Y-m-d', strtotime("+$nbMois months", strtotime($dateTheorique)));

     $i++;

}




so why is he still entering in this loop even if the date compare is not true?

thx for help.

Try this




//1402474743>=1402891200

while(strtotime(date('Y-m-d'))>=strtotime($dateTheorique)  || $i<5)

{

.......

}

Not better :confused:

but thx for help :)

try AND instead of OR,


while(date('Y-m-d')>=date('Y-m-d',strtotime($dateTheorique) && $i<5)

I don’t know if that will work for you, but based on what you said, [color=#880000][size=2]//I added $i condition to avoid infinite loop[/size][/color]

you’r right… i’m stupid.

and now it’s working… so I don’t know what’s wrong before I added this $i condition :P

thx.

And have a good day all.

Vote my answer up. :)

I have a similar problem with my code, The if condition is ‘true’ while $current_date greater than $__begin and $current_date smaller than $_end, but always false when $current_date equal to $__begin or equal to $__end.

Is there any wrong syntax?

Here is the code:

    public function getCurrentAcadYear(){


        &#036;current_date=new DateTime();


        


        &#036;LastRecord=&#036;this-&gt;find(array('order'=&gt;'id DESC'));


        if(&#036;LastRecord){ //jika tidak kosong


            &#036;last_id=&#036;LastRecord-&gt;id;


            while (&#036;last_id){


                &#036;begin=&#036;LastRecord-&gt;begin;


                &#036;_begin=date('Y-m-d', strtotime(str_replace(&quot;,&quot;, &quot;&quot;, &#036;begin)));


                &#036;__begin = new DateTime(&#036;_begin);


                &#036;end=&#036;LastRecord-&gt;end;


                &#036;_end=date('Y-m-d', strtotime(str_replace(&quot;,&quot;, &quot;&quot;, &#036;end)));


                &#036;__end = new DateTime(&#036;_end);


                if((&#036;current_date&gt;=&#036;__begin) AND (&#036;current_date&lt;=&#036;__end))


                    return &#036;last_id;


                else {


                    &#036;last_id--;


                    &#036;LastRecord=&#036;this-&gt;findByPK(array('id'=&gt;&#036;last_id));


                }


            }


        }


        else


            echo 'Tidak ada tahun akademik yg dapat dipilih';


    }