Php 状态持续时间(以每天百分比表示)

Php 状态持续时间(以每天百分比表示),php,algorithm,date,Php,Algorithm,Date,我需要算法方面的帮助。 一般来说,我有如下数组: **Status** (3 options available), **Start** (in timestamp format), **Duration** (in seconds) **Status**, **Start**, **Duration** **Status**, **Start**, **Duration** and so on......... 持续时间可能从几分钟到几天。我想计算一下,每种状态消耗了多少时间 date1 |

我需要算法方面的帮助。 一般来说,我有如下数组:

**Status** (3 options available), **Start** (in timestamp format), **Duration** (in seconds)
**Status**, **Start**, **Duration**
**Status**, **Start**, **Duration**
and so on.........
持续时间可能从几分钟到几天。我想计算一下,每种状态消耗了多少时间

date1 | %of status1 | %of status2 | %of status3
date2 | %of status1 | %of status2 | %of status3
所需的最终产出

2014-09-08 | 73 | 22 | 5
2014-09-07 | 100 | 0 | 0
2014-09-06 | 46 | 34 | 20
我没有任何代码可以显示,因为我无法理解算法,这是如何实现的。任何帮助都将不胜感激


我的输入是CSV文件,我将用PHP编程,但任何语言的帮助都将不胜感激。

如果有人感兴趣,我已经设法创建了算法

<?php
//prepare my $csv array as in question

$final = array();
foreach($csv as $key => $val){

    //start + duration, is still the same day?
    if (date('Y-m-d', $csv[$key]["start"]) == date('Y-m-d', $csv[$key]["start"]+$csv[$key]["duration"])) {
        $final[date('Y-m-d', $csv[$key]["start"])][$csv[$key]["status"]] += $csv[$key]["duration"];
    }
    //we have to split duration of status over days
    else{
        //timestamp of upcomming midnight
        $midnight = mktime(0, 0, 0, date('m', $csv[$key]["start"]), date('d', $csv[$key]["start"])+1);
        $tillMidnightDuration = $midnight - $csv[$key]["start"];
        $afterMidnightDuration = $csv[$key]["duration"]- $tillMidnightDuration;

        $final[date('Y-m-d', $csv[$key]["start"])][$csv[$key]["status"]] += $tillMidnightDuration;

        $day=0;
        //handle after midnight duration
        while($afterMidnightDuration>0){
            //after midnight- next day
            $day++;
            // 86400 is one full day in seconds
            if($afterMidnightDuration > $secondsPerDay){
                $final[date('Y-m-d', mktime(0, 0, 0, date('m', $csv[$key]["start"]), date('d', $csv[$key]["start"])+$day))][$csv[$key]["status"]] += $secondsPerDay;
                $afterMidnightDuration = $afterMidnightDuration - $secondsPerDay;
            }
            // not full day
            else{
                $final[date('Y-m-d', mktime(0, 0, 0, date('m', $csv[$key]["start"]), date('d', $csv[$key]["start"])+$day))][$csv[$key]["status"]] += $afterMidnightDuration;
                $afterMidnightDuration = 0;
            }
        }
    }
}
?>


现在在$final数组中,我将day作为键,每个状态的持续时间(秒)

按“日期”分组行,获取它们的持续时间,确定secondPerDay的百分比?但日期/时间偏移等可能很棘手。什么是“开始”是23:59:55,持续时间是10秒。这需要在几天内分开吗?一天的每一个组合都是100%吗?在提供一个合适的答案之前,大量信息丢失是的,23:59:55和持续10秒的情况是可能的。我尽力提供尽可能多的信息,这将有助于解决我的问题。如果需要更多,我将尝试提供更多。在这种情况下,您必须以某种形式的列表/数组为每个状态类型构建“每天使用量”。鉴于预期的“必需计算”,您不能仅使用您拥有的信息。它需要处理。