PHP:从日期中获取接下来的 13 个日期?

发布于 2024-08-09 19:56:28 字数 608 浏览 6 评论 0原文

我正在尝试获取一个日期加上接下来的 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 技术交流群。

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

发布评论

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

评论(3

少女情怀诗 2024-08-16 19:56:28

我会使用 strtotime

$start = strtotime($s_row['schedule_start_date']);
$dates=array();
for($i = 1; $i<=14; $i++)
{
    array_push($dates,date('Y-m-d', strtotime("+$i day", $start)));
}
print_r($dates);

I would use strtotime

$start = strtotime($s_row['schedule_start_date']);
$dates=array();
for($i = 1; $i<=14; $i++)
{
    array_push($dates,date('Y-m-d', strtotime("+$i day", $start)));
}
print_r($dates);
2024-08-16 19:56:28

由于夏令时切换,您的日期相同。添加 24*60*60 秒来查找第二天并不安全,因为一年中有 2 天的秒数较多/较少。当您从夏季时间切换到冬季时间时,每天会增加 1 小时。因此,当天的时间为 25*60*60 秒,这就是代码中未切换的原因。

您可以通过 mktime()。例如:

## calculate seconds from epoch start for tomorrow
$tomorrow_epoch = mktime(0, 0, 0, date("m"), date("d")+1, date("Y"));
## format result in the way you need
$tomorrow_date = date("M-d-Y", $tomorrow_epoch);

或者您的代码的完整版本:

$dates = array();
$now_year = date("Y");
$now_month = date("m");
$now_day = date("d");
for($i = 0; $i < 14; $i++) {
    $next_day_epoch = mktime(0, 0, 0, $now_month, $now_day + $i, $now_year);
    array_push(
        $dates,
        date("Y-m-d", $next_day_epoch)
    );
}

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 be 25*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:

## calculate seconds from epoch start for tomorrow
$tomorrow_epoch = mktime(0, 0, 0, date("m"), date("d")+1, date("Y"));
## format result in the way you need
$tomorrow_date = date("M-d-Y", $tomorrow_epoch);

Or the full version for your code:

$dates = array();
$now_year = date("Y");
$now_month = date("m");
$now_day = date("d");
for($i = 0; $i < 14; $i++) {
    $next_day_epoch = mktime(0, 0, 0, $now_month, $now_day + $i, $now_year);
    array_push(
        $dates,
        date("Y-m-d", $next_day_epoch)
    );
}
苍景流年 2024-08-16 19:56:28

我推荐类似的东西:

for($i=1;$i<=14;$i++){
     echo("$i day(s) away: ".date("m/d/Y",strtotime("+$i days")));
}

I recommend something like:

for($i=1;$i<=14;$i++){
     echo("$i day(s) away: ".date("m/d/Y",strtotime("+$i days")));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文