代码之家  ›  专栏  ›  技术社区  ›  Spudley Pat

PHP的DatePeriod类给出了不准确的结果[关闭]

  •  0
  • Spudley Pat  · 技术社区  · 7 年前

    我有以下简单的PHP代码:

    $timeRange = new DatePeriod(
        new DateTime('09:00:00'),
        new DateInterval('PT30M'),
        new DateTime('18:00:00')
    );
    
    foreach ($timeRange as $time) {
        $options[$time->format('H:m')] = $time->format('H:m');
    }
    

    我希望能得到一份从 09:00, 09:30, 10:00, 10:30 16:30, 17:00, 17:30, 18:00 .

    我得到的是一张时间列表:

    09:10, 10:10, 11:10, 12:10, 13:10, 14:10, 15:10, 16:10, 17:10
    

    两者是一致的;例如,它不是基于我运行代码的时间。

    我还试着给数据添加一个硬编码的日期 DateTime

    new DateTime('2018-01-01 09:00:00')
    

    我无法解释这种行为。这似乎是完全错误的。有人能解释一下吗,告诉我要怎么做才能解决这个问题。

    当然,我可以恢复使用时间戳;我已经有代码了。但我想知道这是怎么回事 DatePeriod

    1 回复  |  直到 7 年前
        1
  •  3
  •   Alex Howansky    7 年前
    $options[$time->format('H:m')] = $time->format('H:m');
    

    m i

    $options[$time->format('H:i')] = $time->format('H:i');