duandongji2231 2015-06-04 00:12
浏览 68
已采纳

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

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

展开全部

  • 写回答

3条回答 默认 最新

  • dongyumiao5210 2015-06-04 00:17
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部