You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用PHP计算每个PhaseId的业务阶段持续时间?

计算工作流各阶段持续时间的PHP实现

原始数据

array (size=8)
  0 => 
    array (size=5)
      'entity_id' => int 571962
      'time' => int 1671101084788
      'time to datetime' => string '2022-12-15 11:44:44' (length=19)
      'PhaseId_new' => string 'Close' (length=5)
      'PhaseId_old' => string 'Accept' (length=6)
  1 => 
    array (size=5)
      'entity_id' => int 571962
      'time' => int 1671100537178
      'time to datetime' => string '2022-12-15 11:35:37' (length=19)
      'PhaseId_new' => string 'Accept' (length=6)
      'PhaseId_old' => string 'Fulfill' (length=7)
  2 => 
    array (size=5)
      'entity_id' => int 571962
      'time' => int 1671100012012
      'time to datetime' => string '2022-12-15 11:26:52' (length=19)
      'PhaseId_new' => string 'Fulfill' (length=7)
      'PhaseId_old' => string 'Review' (length=6)
  3 => 
    array (size=5)
      'entity_id' => int 571962
      'time' => int 1671099984979
      'time to datetime' => string '2022-12-15 11:26:24' (length=19)
      'PhaseId_new' => string 'Review' (length=6)
      'PhaseId_old' => string 'Accept' (length=6)
  4 => 
    array (size=5)
      'entity_id' => int 571962
      'time' => int 1671099802675
      'time to datetime' => string '2022-12-15 11:23:22' (length=19)
      'PhaseId_new' => string 'Accept' (length=6)
      'PhaseId_old' => string 'Fulfill' (length=7)
  5 => 
    array (size=5)
      'entity_id' => int 571962
      'time' => int 1671027321749
      'time to datetime' => string '2022-12-14 15:15:21' (length=19)
      'PhaseId_new' => string 'Fulfill' (length=7)
      'PhaseId_old' => string 'Approve' (length=7)
  6 => 
    array (size=5)
      'entity_id' => int 571962
      'time' => int 1671011168777
      'time to datetime' => string '2022-12-14 10:46:08' (length=19)
      'PhaseId_new' => string 'Approve' (length=7)
      'PhaseId_old' => string 'Log' (length=3)
  7 => 
    array (size=5)
      'entity_id' => int 571962
      'time' => int 1671011166077
      'time to datetime' => string '2022-12-14 10:46:06' (length=19)
      'PhaseId_new' => string 'Log' (length=3)
      'PhaseId_old' => null

已完成的分组代码

$result = array();
foreach ($data as $element) {
    $result[$element['entity_id']][] = $element;
}

需求说明

需要计算Log、Approve、Fulfill、Accept、Review这几个阶段的总持续时间,计算规则如下:

  • 每条记录代表一次阶段切换:从PhaseId_old切换到PhaseId_new,切换时间为time字段值
  • 单个阶段的持续时间 = 离开该阶段的时间 - 进入该阶段的时间;若阶段重复进入,需累加所有时间段的时长
  • 示例计算(与需求一致):
    • Log:1671011168777 - 1671011166077 = 2700
    • Approve:1671027321749 - 1671011168777 = 16152972
    • Fulfill:(1671100537178 - 1671100012012) + (1671099802675 - 1671027321749) = 73006092
    • Accept:(1671101084788 - 1671100537178) + (1671099984979 - 1671099802675) = 729914
    • Review:1671100012012 - 1671099984979 = 27033

工作流说明

工作流阶段切换顺序为:Log(日志)→ Approve(审批)→ Fulfill(执行)→ Accept(验收)→ Review(复核)→ Fulfill(执行)→ Accept(验收)→ Close(关闭),部分阶段会重复进入,需累加各次进入的持续时长。

工作流示意图

工作流示意图

实现方案

  1. 对每个entity_id下的历史记录,按time字段升序排序(从最早到最晚),确保阶段切换顺序正确
  2. 初始化目标阶段的时长统计数组,默认值为0
  3. 遍历排序后的记录,从第1条开始:当前记录的PhaseId_old是刚刚结束的阶段,用当前记录的time减去上一条记录的time,得到该阶段本次的持续时长,累加到对应阶段的统计值中

完整代码

// 按entity_id分组(已完成的代码)
$result = [];
foreach ($data as $element) {
    $result[$element['entity_id']][] = $element;
}

// 计算各阶段持续时间
$phaseDurations = [];
$targetPhases = ['Log', 'Approve', 'Fulfill', 'Accept', 'Review'];

foreach ($result as $entityId => $records) {
    // 按时间升序排序记录(从最早到最晚)
    usort($records, function($a, $b) {
        return $a['time'] - $b['time'];
    });

    // 初始化当前entity的阶段时长
    $phaseDurations[$entityId] = array_fill_keys($targetPhases, 0);

    // 遍历记录,计算阶段时长
    for ($i = 1; $i < count($records); $i++) {
        $currentRecord = $records[$i];
        $prevRecord = $records[$i-1];
        $endedPhase = $currentRecord['PhaseId_old'];

        // 只统计目标阶段的时长
        if (in_array($endedPhase, $targetPhases)) {
            $duration = $currentRecord['time'] - $prevRecord['time'];
            $phaseDurations[$entityId][$endedPhase] += $duration;
        }
    }
}

// 输出结果
var_dump($phaseDurations);

运行结果

array (size=1)
  571962 => 
    array (size=5)
      'Log' => int 2700
      'Approve' => int 16152972
      'Fulfill' => int 73006092
      'Accept' => int 729914
      'Review' => int 27033

内容的提问来源于stack exchange,提问作者executable

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 08:10:27