Php 而循环从一个日期减去一个月到一个给定的日期

Php 而循环从一个日期减去一个月到一个给定的日期,php,mysql,datetime,datetime-format,Php,Mysql,Datetime,Datetime Format,我试着从一个日期向后减去一个月。我写的代码做减法运算,但我不知道为什么它不能完成循环。下面是代码块 $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 = 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 />";
} 

请帮助我找到它没有完成循环的原因

您必须先将它们转换为时间戳进行比较。使用
strotime()
进行此操作-

while(strtotime($lastsaving) < strtotime($date7)) { ... // rest of the code
while(strotime($lastsaving)
您必须首先将它们转换为时间戳进行比较。使用
strottime()
进行此操作-

while(strtotime($lastsaving) < strtotime($date7)) { ... // rest of the code
while(strotime($lastsaving)
更改

 $lastsaving = date("2013-2-9"); 

在这里,您可以看到正在工作的一个:

我上面的那个人也是对的:) 那也行

while(strtotime($lastsaving) < strtotime($date7)) { 
while(strotime($lastsaving)
此处测试:

更改

 $lastsaving = date("2013-2-9"); 

在这里,您可以看到正在工作的一个:

我上面的那个人也是对的:) 那也行

while(strtotime($lastsaving) < strtotime($date7)) { 
while(strotime($lastsaving)

此处测试:

我将
$lastsaving=date(“2013-2-9”);
更改为
$lastsaving=date(“2013-02-09”);
如上文@Danyal sandelo建议的那样我将
$lastsaving=date(“2013-2-9”);
更改为
$lastsaving=date(“2013-02-09”)上述@Danyal Sandelo建议的
-如果
$lastsaving=date(“2015-5-10”),则不起作用;
。如果年份相同,月份不同,它将不会进入循环。不。由于比较的问题,它将无法工作。它将尝试转换它们。当然,这就是为什么我要求他将月份添加为05…@Danyalsandelo that$lastsaving=date(“2013-02-09”);工作了,非常感谢这浪费了我的两天我很高兴它能工作-如果
$lastsaving=date(“2015-5-10”)的话,它将不起作用;
。如果年份相同,月份不同,它将不会进入循环。不。由于比较的问题,它将无法工作。它将尝试转换它们。当然,这就是为什么我要求他将月份添加为05…@Danyalsandelo that$lastsaving=date(“2013-02-09”);工作非常感谢这浪费了我的两天我很高兴它能工作我认为它应该持续到2013-02-10如果你使用
strotime()
我认为它应该持续到2013-02-10如果你使用
strotime()