在循环中添加迄今为止的年份时出现奇怪的问题

发布于 2024-12-01 03:30:03 字数 469 浏览 2 评论 0原文

$start_date = strtotime('2011-08-21');

    for($i=0 ; $i < 5; $i++)
    {
        echo "i = $i  and ";

        $start_date = mktime(0, 0, 0, date("m", $start_date), date("d", $start_date), date("Y", $start_date)+$i);
        echo date('Y-m-d',$start_date)."<br /><hr />";
    }

i = 0 and 2011-08-21
i = 1 and 2012-08-21
i = 2 and 2014-08-21
i = 3 and 2017-08-21
i = 4 and 2021-08-21

我不明白为什么 2012 年之后它不能正确添加。

谢谢

$start_date = strtotime('2011-08-21');

    for($i=0 ; $i < 5; $i++)
    {
        echo "i = $i  and ";

        $start_date = mktime(0, 0, 0, date("m", $start_date), date("d", $start_date), date("Y", $start_date)+$i);
        echo date('Y-m-d',$start_date)."<br /><hr />";
    }

i = 0 and 2011-08-21
i = 1 and 2012-08-21
i = 2 and 2014-08-21
i = 3 and 2017-08-21
i = 4 and 2021-08-21

i didn't understand why after 2012 it doesn't add correctly.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

喜爱纠缠 2024-12-08 03:30:03

工作正常。您在每次迭代中向年份添加一个递增的数字。

i = 0 and 2011-08-21 // 2011 + 0
i = 1 and 2012-08-21 // 2011 + 1
i = 2 and 2014-08-21 // 2012 + 2
i = 3 and 2017-08-21 // 2014 + 3
i = 4 and 2021-08-21 // 2017 + 4

您应该

  • 在每次迭代时+1而不是+$1到年份
  • 不要覆盖$start_date每次迭代

更新(不覆盖)

for($i=0 ; $i < 5; $i++)
{
    echo "i = $i  and ";

    // $new_date holds the updated date without overwriting
    $new_date = mktime(0, 0, 0, date("m", $start_date), date("d", $start_date), date("Y", $start_date)+$i);

    echo date('Y-m-d', $new_date)."<br /><hr />";
}

It is working correctly. You're adding an incrementing number to the year on each iteration.

i = 0 and 2011-08-21 // 2011 + 0
i = 1 and 2012-08-21 // 2011 + 1
i = 2 and 2014-08-21 // 2012 + 2
i = 3 and 2017-08-21 // 2014 + 3
i = 4 and 2021-08-21 // 2017 + 4

You should either

  • +1 instead of +$1 to the year on each iteration
  • OR don't overwrite $start_date on each iteration

Update (without overwriting)

for($i=0 ; $i < 5; $i++)
{
    echo "i = $i  and ";

    // $new_date holds the updated date without overwriting
    $new_date = mktime(0, 0, 0, date("m", $start_date), date("d", $start_date), date("Y", $start_date)+$i);

    echo date('Y-m-d', $new_date)."<br /><hr />";
}
╭⌒浅淡时光〆 2024-12-08 03:30:03

如果您想每次添加一年,也许您应该这样做。

for($i=0 ; $i < 5; $i++)
{ echo "i = $i  and ";
  if($i>0)  $start_date = mktime(0, 0, 0, date("m", $start_date), date("d", $start_date), date("Y", $start_date)+1);
  echo date('Y-m-d',$start_date)."<br /><hr />";
}

Maybe you should do it like this if you want to add one year each time.

for($i=0 ; $i < 5; $i++)
{ echo "i = $i  and ";
  if($i>0)  $start_date = mktime(0, 0, 0, date("m", $start_date), date("d", $start_date), date("Y", $start_date)+1);
  echo date('Y-m-d',$start_date)."<br /><hr />";
}
可可 2024-12-08 03:30:03

下面的行

$start_date = mktime(0, 0, 0, date("m", $start_date), date("d", $start_date), date("Y", $start_date)+$i);

应该是

$start_date = mktime(0, 0, 0, date("m", $start_date), date("d", $start_date), date("Y", $start_date)+1);

否则日期的年份将依次增加 1、2、3、4,因为每次都会修改开始日期。

The following line

$start_date = mktime(0, 0, 0, date("m", $start_date), date("d", $start_date), date("Y", $start_date)+$i);

Should be

$start_date = mktime(0, 0, 0, date("m", $start_date), date("d", $start_date), date("Y", $start_date)+1);

Otherwise the year of the date is being increased by 1 then 2 then 3 the 4, as teh start date is being modified each time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文