【问题标题】:Calculate Average Time Duration in PHP from a csv file. Average time seems 0 milisseconds从 csv 文件计算 PHP 中的平均持续时间。平均时间似乎是 0 毫秒
【发布时间】:2020-01-29 09:58:37
【问题描述】:

我有一个包含这些值的 csv 文件:

Iteration, Duration
0,00:00:02.694414
81,00:00:02.790214
82,00:00:02.933225
83,00:00:03.077099
84,00:00:03.220184
85,00:00:03.363437

在它的第二列我有一个H:i:s.ss 格式的持续时间,我需要计算平均持续时间:sum(Duration)/csv_size

为了做到这一点,我使用了这个功能:

/**
 * convert a DateTimeIntervalIn milliseconds
 * @return int Time in miliseconds
 */
function intervalInMiliseconds(DateInterval $interval) : float
{
    return ($interval->days*86400 + $interval->h*3600 
           + $interval->i*60 + $interval->s)*1000 + ($interval->f*1000);
}

/**
 * Undocumented function
 *
 * @return int
 */
function calculateAverageTime(string $csvFile)
{
    if ( !file_exists($csvFile) ) {
        throw new Exception('File not found.');
    }

    $file = fopen($csvFile,"r");
    $count=0;
    $duration= new DateTime('00:00');
    $durationCalc= clone $duration;
    while(($data = fgetcsv($file, 1000, ",")) !== FALSE){
        $duration->add( DateInterval::createFromDateString($data[1]));
        $count++;
    }

    if($count == 0)
    {
        throw new Exception("No Data In the CSV");
    }

    $duration=$duration->diff($durationCalc);
    $averageSeconds=intervalInMiliseconds($duration)/$count;

    fclose($file);

    return $averageSeconds;
}

$time = calculateAverageTime(__DIR__.'/timestamps.csv');

echo $time;

但我的问题是如何以毫秒为单位格式化DateInterval,以便能够计算以毫秒为单位的平均时间我得到 0,这个结果对我来说似乎不合适。对于上面给出的数据,我希望毫秒的分数而不是 0。

所以你知道如何解决这个问题吗?

编辑 1: 一个快速的 var_dump 显示

  var_dump($datetime);

显示将我的时间戳解析为 DateInterval 失败:

class DateInterval#1 (16) {
  public $y =>
  int(0)
  public $m =>
  int(0)
  public $d =>
  int(0)
  public $h =>
  int(0)
  public $i =>
  int(0)
  public $s =>
  int(0)
  public $f =>
  double(0)
  public $weekday =>
  int(0)
  public $weekday_behavior =>
  int(0)
  public $first_last_day_of =>
  int(0)
  public $invert =>
  int(0)
  public $days =>
  bool(false)
  public $special_type =>
  int(0)
  public $special_amount =>
  int(0)
  public $have_weekday_relative =>
  int(0)
  public $have_special_relative =>
  int(0)
}

那么如何将时间戳从 csv 转换为 DateInterval?

【问题讨论】:

  • 首先尝试通过 DateTime 和间隔处理这个是否有意义......?我会抓住00:00:02.694414 的值,在冒号处拆分它,然后以秒为单位计算相应的值。
  • Somne 实体实际上也在00:02:10.586603 中。
  • 是的,那是0 * 60 * 60 + 2 * 60 + 10.586603 秒然后......

标签: php time average intervals


【解决方案1】:

将毫秒/微秒与 DateTime 相加可能会导致不准确。 我会完全按照@04FS 评论中的建议去做。

原理:

$durations = [
  "00:00:02.694414",
  "00:00:02.790214",
  "00:00:02.933225",
  "00:00:03.077099",
  "00:00:03.220184",
  "00:00:03.363437"
];

$sum = 0.0;
foreach($durations as $duration){
  list($h,$m,$s) = explode(':',$duration);
  $sum += $h * 3600 + $m * 60 + $s;
}
$avg = $sum/count($durations);
echo $avg;  //3.0130955

array_column() 函数可用于从二维数组 (csv) 中提取持续时间。

【讨论】:

    猜你喜欢
    • 2022-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-19
    • 1970-01-01
    • 2019-02-17
    • 2022-08-02
    相关资源
    最近更新 更多