Am trying to subtract one month from a date backwards to a given date. The code i have written does the subtraction but i don't know why it doesn't complete the loop. below is the code block
$date7 = date('Y-m-10');
$lastsaving = date("2013-2-9");
while($lastsaving < $date7){
$newdate = strtotime ( '-1 month' , strtotime ( $date7 ) ) ;
$date7 = date ( 'Y-m-d' , $newdate );
echo $date7;
echo "<br />";
}
result i get is
2015-05-10
2015-04-10
2015-03-10
2015-02-10
2015-01-10
2014-12-10
2014-11-10
2014-10-10
2014-09-10
2014-08-10
2014-07-10
2014-06-10
2014-05-10
2014-04-10
2014-03-10
2014-02-10
2014-01-10
2013-12-10
please help me find the reason it's not completing the loop
change
$lastsaving = date("2013-2-9");
to
$lastsaving = date("2013-02-9");
Here, you can see the working one : http://codepad.org/uI0R6TvC
The guy above me is right as well :) that would work too
while(strtotime($lastsaving) < strtotime($date7)) {
tested here : http://codepad.org/OY36ij3U
You have to convert them to timestamp first for comparing.Use strtotime()
for this -
while(strtotime($lastsaving) < strtotime($date7)) { ... // rest of the code
i changed $lastsaving = date("2013-2-9");
to $lastsaving = date("2013-02-09");
as suggested by @Danyal Sandeelo above