douyou7797 2015-10-05 15:07
浏览 49
已采纳

PHP日期递增错误

I am trying to create a calendar where the date increments and each date is clickable, which links to a search. The strange part is that the date stops at 25th October, and stops incrementing. i.e 24th, 25th, 25th, 25th...

Its doesn't matter which day the calendar started with (been staring at it for a few days), but at 25th it stops incrementing.

Grateful for any advice.(The 2nd part after the gap is probably irrelevant, but including it in case there could be any link)

for ($i = 1; $i <= 30; $i++){ 
    $date = date("d-m-Y", strtotime($date) + 86400);
    array_push($array_date, $date);
    $separatedate = explode('-', $date);   
    $getday = date("l", strtotime($date));
    print "<button class='submitsearch btn' value=$array_date[$i]>" . ltrim($separatedate[0], '0') . "<br>" . $getday . "<br></button>";

    if (!checkdate($separatedate[1] , $separatedate[0]+1 , $separatedate[2])) {
        $nextmonth = date("F", strtotime($date) + 86400);
       print "<strong>". $nextmonth . "</strong><hr/>";
    }
}
  • 写回答

1条回答 默认 最新

  • doushi1473 2015-10-05 15:10
    关注

    This is the problem:

    $date = date("d-m-Y", strtotime($date) + 86400);
    

    You appear to be relying on that to increment the date. Usually, that will be fine... but it isn't when we have a 25 hour day, due to daylight saving time changes.

    I suggest you use date/time arithmetic functions (e.g. date_add) designed to add a day, rather than adding 24 hours. Or make sure all arithmetic is done in UTC, which won't have any time zone changes. In general, I would try to avoid performing any more string conversions than you really need to: keep a variable representing the date/time in an idiomatic way, and perform arithmetic on that - then just format that variable when you need to. I don't see any need to call strtotime anywhere, if you do this right.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?