代码之家  ›  专栏  ›  技术社区  ›  xtofl Adam Rosenfield

日期时间算法

  •  3
  • xtofl Adam Rosenfield  · 技术社区  · 14 年前

    DateInterval PHP类。我真正想要的是两个时间间隔的秒数 DateTime

    $t1 = new DateTime( "20100101T1200" );
    $t2 = new DateTime( "20100101T1201" );
    // number of seconds between t1 and t2 should be 60
    
    echo "difference in seconds: ".$t1->diff($t2)->format("%s");
    

    但我得到的是零。是 可以

    1 回复  |  直到 14 年前
        1
  •  9
  •   Denis 'Alpheus' Cahuk    14 年前

    如果你只是想快速的获得秒数,你也可以使用

    $diff = abs($t1->getTimestamp() - $t2->getTimestamp());
    

    您的代码返回0,因为实际 1分钟 %我 格式,您将得到1,这是正确的 $t1 $t2 .