drdr123456 2015-12-02 16:09
浏览 18
已采纳

应该是新月的增加日期

I have a PHP script which records things based on the day. So it will have a weekly set of inputs you would enter.

I get the data correctly, but when i do $day ++; it will increment the day, going passed the end of the month without ticking the month.

example:

//12/29
//12/30
//12/31
//12/32
//12/33

Where it should look like

//12/29
//12/30
//12/31
//01/01
//01/02

My script is as follows:

$week = date ("Y-m-d", strtotime("last sunday"));
$day = $week;
$run = array(7);  //this is actually defined in the data posted to the script, which is pretty much just getting the value of the array index for the query string.
foreach( $run as $key=>$value)
{
    $num = $key + 1;
    $items[] = "($num, $user, $value, 'run', '$day')";
    echo "".$day;
    $day ++;
}

Should I be manipulating the datetime differently for day incrementations?

  • 写回答

3条回答 默认 最新

  • doujiu4643 2015-12-02 16:18
    关注

    You refer to $day as a "datetime" but it is just a string - that is what date() returns. So when you do $day++ you are adding 1 to "2015-12-02". PHP will do everything it can to make "2015-12-02" into a number and then add 1 to it, which is not date math. Here is a simple example:

    <?php
    $name = "Fallenreaper1";
    $name++;
    echo $name
    ?>
    

    This will output:

    Fallenreaper2
    

    This is how I would do it, using an appropriate data type (DateTime):

    <?php
    $day = new DateTime('last sunday');
    
    $run = array(7);
    foreach ($run as $key => $value) {
      $num = $key + 1;
      $dayStr = $day->format('Y-m-d');
      $items[] = "($num, $user, $value, 'run', '$dayStr')";
      echo $dayStr;
      $day->modify('+1 day');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来