dounie0889 2012-07-13 11:00
浏览 28
已采纳

计算给定时间戳集的持续时间

I have a set of intime and outtime, and i want to calculate the duration between the intime and outtime and add all the duration to get the total duration.
example:

Array
    (
    [1] => Array
        (
            ['intime'] => 1342086993
            ['outime'] => 1342087585
        )

    [3] => Array
        (
            ['intime'] => 1342087669
            ['outtime] => 1342096165
        )

    [5] => Array
        (
            ['intime'] => 1342096339
            ['outime'] => 1342096345
        )
    )

$total_duration = 0;
foreach($logArray as $log)
{
  $duration = $log['outime'] - $log['intime'];
  $total_duration += $duration;
}

$duration_hrs = $total_duration/3600;
echo 'Time spend in hrs :'.$duration_hrs;

but this is not giving me the proper duration. is there any other way to do so.

  • 写回答

4条回答 默认 最新

  • dongpang2029 2012-07-13 11:18
    关注

    Your syntax free script, works for me fine:

    <?php
    
        $logArray = Array (
            1 => Array (
                'intime' => 1342086993,
                'outime' => 1342087585
            ),
            3 => Array (
                'intime' => 1342087669,
                'outime' => 1342096165
            ),
            5 => Array (
                'intime' => 1342096339,
                'outime' => 1342096345
            )
        );
    
        $total_duration = 0;
        foreach( $logArray as $log ) {
          $duration = $log[ 'outime' ] - $log[ 'intime' ];
          $total_duration += $duration;
            echo $duration / 3600 . "<br />";
        }
    
        $duration_hrs = $total_duration / 3600;
        echo 'Time spend in hrs: ' . $duration_hrs;
    
    ?>
    

    And the output:

    0.16444444444444
    2.36
    0.0016666666666667
    Time spend in hrs: 2.5261111111111 
    

    Seems like your time format is not right: 13, July 2012 04:04:32 PM should be 13. July 2012 04:04:32 PM for

    strtotime('13. July 2012 04:04:32 PM')
    

    to work right. It should give 1342195472 instead of you 1342168834. So check your time formats that are generating wrong microtimes

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。