While循环从给定日期到给定日期减去一个月
While Loop Subtracting one month from a date to a given day
我正在尝试从给定日期向后减去一个月。我写的代码做了减法,但我不知道为什么它没有完成循环。下面是代码块
$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 />";
}
我得到的结果是
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
请帮我找出它没有完成循环的原因
您必须先将它们转换为 timestamp,以便 comparing.Use strtotime()
为此 -
while(strtotime($lastsaving) < strtotime($date7)) { ... // rest of the code
改变
$lastsaving = date("2013-2-9");
到
$lastsaving = date("2013-02-9");
在这里,您可以看到正在运行的一个:
http://codepad.org/uI0R6TvC
我上面的那个人也是对的:)
那也行
while(strtotime($lastsaving) < strtotime($date7)) {
我按照上面@Danyal Sandeelo 的建议将 $lastsaving = date("2013-2-9");
更改为 $lastsaving = date("2013-02-09");
我正在尝试从给定日期向后减去一个月。我写的代码做了减法,但我不知道为什么它没有完成循环。下面是代码块
$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 />";
}
我得到的结果是
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
请帮我找出它没有完成循环的原因
您必须先将它们转换为 timestamp,以便 comparing.Use strtotime()
为此 -
while(strtotime($lastsaving) < strtotime($date7)) { ... // rest of the code
改变
$lastsaving = date("2013-2-9");
到
$lastsaving = date("2013-02-9");
在这里,您可以看到正在运行的一个: http://codepad.org/uI0R6TvC
我上面的那个人也是对的:) 那也行
while(strtotime($lastsaving) < strtotime($date7)) {
我按照上面@Danyal Sandeelo 的建议将 $lastsaving = date("2013-2-9");
更改为 $lastsaving = date("2013-02-09");