【问题标题】:PHP: get next 13 dates from date?PHP:从日期开始获取接下来的 13 个日期?
【发布时间】:2010-12-10 23:24:51
【问题描述】:

我正在尝试获取一个日期加上接下来的 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 重复了。我不知道为什么?

我做错了什么?

谢谢!!

【问题讨论】:

    标签: php date time php4


    【解决方案1】:

    由于daylight saving time switch,您的日期相同。添加 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)
        );
    }
    

    【讨论】:

    • 那是因为,那我应该怎么做呢?谢谢。
    【解决方案2】:

    我会使用 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);
    

    【讨论】:

      【解决方案3】:

      我推荐类似的东西:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-06-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-05
        相关资源
        最近更新 更多