PHP:从日期中获取接下来的 13 个日期?
我正在尝试获取一个日期加上接下来的 13 个日期的数组,以获得从给定日期开始的 14 天时间表。
这是我的功能:
$time = strtotime($s_row['schedule_start_date']); // 20091030
$day = 60*60*24;
for($i = 0; $i<14; $i++)
{
$the_time = $time+($day*$i);
$date = date('Y-m-d',$the_time);
array_push($dates,$date);
}
但它似乎在月份切换时重复日期..
这就是我得到的:
2009-10-30|2009-10-31|2009-11-01|2009-11-01|2009-11-02|2009-11-03|2009-11-04|2009-11-05 |2009-11-06|2009-11-07|2009-11-08|2009-11-09|2009-11-10|2009-11-11
请注意,2009-11-01 是重复的。我不明白为什么?
我做错了什么?
谢谢!!
I am trying to get an array of a date plus the next 13 dates to get a 14 day schedule starting from a given date.
here is my function:
$time = strtotime($s_row['schedule_start_date']); // 20091030
$day = 60*60*24;
for($i = 0; $i<14; $i++)
{
$the_time = $time+($day*$i);
$date = date('Y-m-d',$the_time);
array_push($dates,$date);
}
But it seems to be repeating a date when the month switches over..
this is what I get:
2009-10-30|2009-10-31|2009-11-01|2009-11-01|2009-11-02|2009-11-03|2009-11-04|2009-11-05|2009-11-06|2009-11-07|2009-11-08|2009-11-09|2009-11-10|2009-11-11
Notice that 2009-11-01 is repeated. I cannot figure out why?
What am I doing wrong?
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会使用 strtotime
I would use strtotime
由于夏令时切换,您的日期相同。添加
24*60*60
秒来查找第二天并不安全,因为一年中有 2 天的秒数较多/较少。当您从夏季时间切换到冬季时间时,每天会增加 1 小时。因此,当天的时间为25*60*60
秒,这就是代码中未切换的原因。您可以通过
mktime()
。例如:或者您的代码的完整版本:
You have the same date because of daylight saving time switch. It's not safe to add
24*60*60
seconds to find next day, because 2 days in the year have more/less seconds in them. When you switch from summer to winter time you are adding 1 hour to a day. So it'll be25*60*60
seconds in that day, that's why it's not switched in your code.You can do your calculation by
mktime()
. For example:Or the full version for your code:
我推荐类似的东西:
I recommend something like: