doulong1987 2018-08-11 17:22
浏览 45
已采纳

PHP,将gmdate添加到gmdate

I have a code that looks like this:

        $dagtidhelg = gmdate('H:i', $diffMorning) . "
";
        $kvallstidhelg = gmdate('H:i', $diffNight);

This code runs several times per page since its runt every time a row is loaded from mysql. It can return a time value ie 08:15 and 09:30. This is the lenght of two work sessions.

That works great but now Im stuck, I want to display the total of every work session at the bottom. I have tried this:

        $dagtidhelgtotal = $dagtidhelgtotal + $dagtidhelg;
        $kvalltidhelgtotal = $kvalltidhelgtotal + $kvallstidhelg;

But that only adds the hours togheter, it wont even display the : So Im guessing that Im doing this totaly wrong.

How can I add these times togheter? Maybe convert them to minutes, then add them all togheter?

  • 写回答

1条回答 默认 最新

  • duangouyan3328 2018-08-11 19:22
    关注

    Add duration together is simple .But you must keep in mind that duration and date are two things completely different. You can write 100:08 for duration but not for date. If your purpose is to keep a duration counter on(one or) every page(s) you need to build a system based on $_SESSION variable. To add two duree you can proceed like this:

    function addDuration($duration1,$duration2){
    
            $result=array_map(function($x,$y){return sprintf("%'.02d", $x+$y);},explode(':',$duration1),explode(':',$duration2));
            if($result[1]>60){
                $result[0]=sprintf("%'.02d",$result[0]+(int)$result[1]/60);
                $result[1]=sprintf("%'.02d",$result[1]%60);
            }elseif($result[1]==60){
                $result[1]="00";
                $result[0]=sprintf("%'.02d",$result[0]+1);
            }
            return  join(':',$result);
    }
    

    and the usage in your case could be:

     $dagtidhelgtotal = addDuration($dagtidhelgtotal,$dagtidhelg);
    

    if we suppose that

    $dagtidhelgtotal ==='100:09' && $dagtidhelg === '08:08'
    

    then after the addition $dagtidhelgtotal will be equal to '108:17';

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示