duandongji2231 2015-06-04 08: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 08: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条)

报告相同问题?

悬赏问题

  • ¥15 用C语言实现语音的读取及播放
  • ¥15 poi怎么在指定位置之后生成多个表格
  • ¥15 nginx在location设置了client_max_body_size依旧报错413
  • ¥15 C#添加WCF服务引用客户端调用方法没有反应
  • ¥15 stm32f103c8t6与esp8266
  • ¥15 使用Hadoop的MapReduce program
  • ¥15 Python发生IndexError错误如何解决?
  • ¥15 如何用matlab搭建激光器
  • ¥15 51单片机两路互补SPWM输出全桥逆变
  • ¥15 outlook左边用户名下拉为啥打不开了