在循环中添加迄今为止的年份时出现奇怪的问题
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它工作正常。您在每次迭代中向年份添加一个递增的数字。
您应该
+1
而不是+$1
到年份$start_date
每次迭代更新(不覆盖)
It is working correctly. You're adding an incrementing number to the year on each iteration.
You should either
+1
instead of+$1
to the year on each iteration$start_date
on each iterationUpdate (without overwriting)
如果您想每次添加一年,也许您应该这样做。
Maybe you should do it like this if you want to add one year each time.
下面的行
应该是
否则日期的年份将依次增加 1、2、3、4,因为每次都会修改开始日期。
The following line
Should be
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.