即使间隔应该只有几秒,DateTime :: add也会增加几小时

时间:2014-03-13 16:32:53

标签: php datetime

这来自我之前关于获取指定数据集的平均时间间隔的问题,[位于此处] [1]。我将再次发布整个功能:

function getATBData($siteID, $fromDate, $toDate)
{
    global $pdo;

    $ATBarray = array();
    $maxATB;
    $minATB;
    $avgATB;
    $totalATB=new DateTime("@0");
    $totalEvents=0;
    $timetable;

    $query = "SELECT id, siteID, start_time, end_time FROM atb_log WHERE siteID=:siteID AND (start_time BETWEEN :fromDate AND :toDate) AND (end_time BETWEEN :fromDate AND :toDate)";
    $stmt = $pdo->prepare($query);
    $stmt->bindParam(":siteID", $siteID);
    $stmt->bindParam(":fromDate", $fromDate);
    $stmt->bindParam(":toDate", $toDate);
    $stmt->execute();

    foreach ($stmt as $row)
    {
        $timeDiff = date_diff(new DateTime($row['start_time']),new DateTime($row['end_time']), true); //force absolute
        if(!isset($maxATB) OR dateIntervalInSeconds($timeDiff) > dateIntervalInSeconds($maxATB))
            $maxATB = $timeDiff;
        if(!isset($minATB) OR dateIntervalInSeconds($timeDiff) < dateIntervalInSeconds($minATB))
            $minATB = $timeDiff;
        $totalATB->add($timeDiff);
        echo "added " . $timeDiff->format("%H:%I:%S") . " total is now: " . $totalATB->format("H:i:s") . "<br />";
        $totalEvents++;
    }

    if($totalEvents!=0)
    {
        $avgATB = average_time($totalATB->format("H:i:s"),$totalEvents,0);
    }
    else
    {
        $avgATB=0;
        $maxATB=new DateInterval('PT0S');
        $minATB=new DateInterval('PT0S');
    }
    //$avgSeconds = new DateInterval("PT" . $avgATB . "S");
    $ATBarray['max'] = $maxATB->format("%H:%I:%S");
    $ATBarray['min'] = $minATB->format("%H:%I:%S");
    $ATBarray['avg'] = $avgATB;
    $ATBarray['total'] = $totalATB->format("H:i:s");
    $ATBarray['events'] = $totalEvents;

    return $ATBarray;

}

鉴于此功能,我添加了一个输出语句,试图调试为什么我的总时间(当大部分值都是少量秒数)得到如此大的时间间隔时,为什么它是&#39输出:

added 00:00:02 total is now: 01:00:02
added 00:00:00 total is now: 02:00:02
added 00:00:01 total is now: 03:00:03
added 00:00:01 total is now: 04:00:04
added 00:00:00 total is now: 05:00:04
added 00:00:02 total is now: 06:00:06
added 00:00:00 total is now: 07:00:06

等等。所以看起来,尽管只需几秒钟就可以添加时间,但每次增加一小时。上面add()$timeDiff的来电是我添加的方式。

所以问题是 - 是否有一种不同的方式来调用add()函数,这样它只会添加秒数?我打电话不正确吗?

2 个答案:

答案 0 :(得分:1)

正如我在回答您的其他问题时写的,这是DST(夏令时)问题。在美国,DST已经开始;在欧洲没有。

试试这段代码:

timecheck("Europe/Amsterdam");
timecheck("America/Los_Angeles");

function timecheck($timezone) {
date_default_timezone_set($timezone);

$totalATB=new DateTime("@0");
$t1 = "2014-01-01 17:30:00";
$t2 = "2014-01-01 17:35:00";

$dt1 = new DateTime($t1);
$dt2 = new DateTime($t2);

$timeDiff = date_diff($dt1, $dt2, true);

printf("[%s] Starting with with: %s\n", $timezone, $totalATB->format("H:i:s"));
$totalATB->add($timeDiff);
printf("[%s] added %s, total is now: %s\n", $timezone, $timeDiff->format("%H:%I:%S"), $totalATB->format("H:i:s"));
}

输出:

[Europe/Amsterdam] Starting with with: 00:00:00
[Europe/Amsterdam] added 00:05:00, total is now: 00:05:00
[America/Los_Angeles] Starting with with: 00:00:00
[America/Los_Angeles] added 00:05:00, total is now: 01:05:00

答案 1 :(得分:1)

Hm,以秒为单位的平均差异,为什么PHP数据库代码可以提供给您:

SELECT 
  SEC_TO_TIME(MAX(TIME_TO_SEC(TIMEDIFF(end_time,start_time)))) AS max_timediff,
  SEC_TO_TIME(MIN(TIME_TO_SEC(TIMEDIFF(end_time,start_time)))) AS min_timediff,
  SEC_TO_TIME(AVG(TIME_TO_SEC(TIMEDIFF(end_time,start_time)))) AS avg_timediff,
  SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(end_time,start_time)))) AS sum_timediff,
  COUNT(id) as total_events
FROM atb_log 
WHERE 
  siteID=:siteID 
  AND start_time > :fromDate
  AND end_time < :toDate

根据您的喜好格式化那些min / max / avg /秒数。