Add PHP and MySQL hours

0

I took a project in progress, a tracking system that has a working day calculation. The system calculates certain, but sometimes adding up time of the vehicle stopped and walking sum 24 hours 59 minutes.

I'll try to put some of the code here

    $html .= "<table width='70%' align='center' border='1' cellspacing='0' cellpadding='0' style='background:#ffffff;border-collapse:collapse;font-family:verdana;font-size:12px;' bordercolor='#CCCCCC'>";
$html .= "<tr>";
$html .= "<td width='20%' align='center'><img src='../../imagens/logop.png'></td>";
$html .= "<td align='center'><font size='3'><strong>JORNADA DE TRABALHO<br>".BuscaVeiculo($cd_veiculo)."</strong></font><br>".$dt_ini." - ".$dt_fim."</td>";
$html .= "</tr>";
$html .= "</table>";
$html .= "<table width='70%' align='center' border='0' cellspacing='1' cellpadding='1' style='font-family:verdana;font-size:11px;'>";
// INI DETALHADO////
if($cka == 0){
$html .= "<tr bgcolor='#DDD7CA'>";
$html .= "<td><strong>Situação</strong></td>";
$html .= "<td><strong>Inicial</strong></td>";
$html .= "<td><strong>Fim</strong></td>";
$html .= "<td><strong>Tempo</strong></td>";
$html .= "</tr>";
    $aa = 0; $pp = 0;
    $Inicio         = $dt_ini;
    $Fim            = $dt_fim;
    $Veiculo        = $cd_veiculo;
    $Inicial        = $dt_ini." 00:00";
    $Final          = $dt_fim." 23:59";
    $Inicial        = DtBrToDtEua($Inicial, 4);
    $Final          = DtBrToDtEua($Final, 4);


    $SQL = "SELECT dt_hora as dt, ";
    $SQL .= " (sqrt(((vl_latitude - (vl_lati_anterior))*(vl_latitude - (vl_lati_anterior))) + ((vl_longitude - (vl_long_anterior))*(vl_longitude - (vl_long_anterior))))*111) as distancia ";
    $SQL .= " from hist_posicao where cd_veiculo = '". $Veiculo ."' ";
    $SQL .= " and dt_hora >= '". $Inicial ."' and dt_hora <= '". $Final ."' ";
    $SQL .= " order by dt_hora asc ";
    //echo $html .= $SQL;exit;
    $RSS = mysql_query($SQL, $conexao);
    if(mysql_num_rows($RSS) > 0){       
        $RS = mysql_fetch_assoc($RSS);
        $st = 0;
        $dia = date("d", strtotime($RS["dt"]));
        if($RS["distancia"] >= 0.3 && $RS["ig"] == 1){$ini = date("d/m/Y", strtotime($RS["dt"]))." 00:00"; $st = 1;}
        if($RS["distancia"] < 0.3 && $RS["ig"] == 0){$ini = date("d/m/Y", strtotime($RS["dt"]))." 00:00"; $st = 2;}
        if($cor == "#FFFFFF"){$cor = "#DDDDDD";}else{$cor = "#FFFFFF";}
        if(diadasemana(date("d/m/Y", strtotime($RS["dt"]))) != "1" && diadasemana(date("d/m/Y", strtotime($RS["dt"]))) != "7")//se dia semana não for 1 nem 7
            {$corsm = "#ffff99";}else{$corsm = "#33ff66";}//definição para cor da linha para final de semana e dia de semana
        $html .= "<tr bgcolor='".$corsm."'><td colspan='4' align='center'><b>".dsdiadasemana(date("d/m/Y", strtotime($RS["dt"])))." | ".date("d/m/Y", strtotime($RS["dt"]))."</b></td></tr>";//nome do dia da semana e data

        while($RS = mysql_fetch_array($RSS)){
            $x = $x + 1;
            if($dia != date("d", strtotime($RS["dt"]))){
                if($st == 1){
                    //echo $ini." | ".$ultimo ."<br><br><br>";
                    $html .= "<tr bgcolor='".$cor."'><td>Andando</td><td>".$ini."</td><td>".$fim."</td><td>".MinToHrMin(datediff("n", $ini, $ultimo." 23:59"))."</td></tr>";
                    $andando = $andando + datediff("n", $ini, $ultimo." 23:59");
                    $aa = $aa + datediff("n", $ini, $ultimo." 23:59");
                    if(diadasemana($ultimo) != "1" && diadasemana($ultimo) != "7")
                        {$saa = $saa + datediff("n", $ini, $ultimo." 23:59");}else{$fsaa = $fsaa + datediff("n", $ini, $ultimo." 23:59");}
                }
                if($st == 2){
                    //echo $ini." | ".$ultimo ."<br>";          
                    $html .= "<tr bgcolor='".$cor."'><td>Parado</td><td>".$ini."</td><td>".$fim."</td><td>".MinToHrMin(datediff("n", $ini, $ultimo." 23:59"))."</td></tr>";//header
                    $parado = $parado + datediff("n", $ini, $ultimo." 23:59");
                    $pp = $pp + datediff("n", $ini, $ultimo." 23:59");
                    if(diadasemana($ultimo) != "1" && diadasemana($ultimo) != "7")
                        {$spp = $spp + datediff("n", $ini, $ultimo." 23:59");}else{$fspp = $fspp + datediff("n", $ini, $ultimo." 23:59");}
                }
                $html .= "<tr bgcolor='".$cor."'><td colspan='3'><b>Andando no Dia</b></td><td><b>".MinToHrMin($andando)."</b></td></tr>";
                $html .= "<tr bgcolor='".$cor."'><td colspan='3'><b>Parado no Dia</b></td><td><b>".MinToHrMin($parado)."</b></td></tr>";
                if(diadasemana(date("d/m/Y", strtotime($RS["dt"]))) != "1" && diadasemana(date("d/m/Y", strtotime($RS["dt"]))) != "7")
                    {$corsm = "#ffff99";}else{$corsm = "#33ff66";}
                $html .= "<tr bgcolor='".$corsm."'><td colspan='4' align='center'><b>".dsdiadasemana(date("d/m/Y", strtotime($RS["dt"])))." | ".date("d/m/Y", strtotime($RS["dt"]))."</b></td></tr>";

                if($cor == "#FFFFFF"){$cor = "#DDDDDD";}else{$cor = "#FFFFFF";}
                $andando = 0;
                $parado = 0;
                if($RS["distancia"] >= 0.3){$ini = date("d/m/Y", strtotime($RS["dt"]))." 00:00"; $st = 1;}
                if($RS["distancia"] < 0.3){$ini = date("d/m/Y", strtotime($RS["dt"]))." 00:00"; $st = 2;}
            }
            if($x > 1){
                if($RS["distancia"] >= 0.3){
                    if($st == 1){
                        $fim = date("d/m/Y H:i", strtotime($RS["dt"]));
                        $st = 1;
                    }
                    if($st == 2){
                        if($ini != $fim){
                            $html .= "<tr bgcolor='".$cor."'><td>Parado</td><td>".$ini."</td><td>".$fim."</td><td>".MinToHrMin(datediff("n", $ini, $fim))."</td></tr>";
                        }
                        $parado = $parado + datediff("n", $ini, $fim);
                        $pp = $pp + datediff("n", $ini, $fim);
                        if(diadasemana($fim) != "1" && diadasemana($fim) != "7")
                            {$spp = $spp + datediff("n", $ini, $fim);}else{$fspp = $fspp + datediff("n", $ini, $fim);}
                        $ini = $fim;
                        $st = 1;
                    }
                }
                if($RS["distancia"] < 0.03){
                    if($st == 1){
                        if($ini != $fim){
                            $html .= "<tr bgcolor='".$cor."'><td>Andando</td><td>".$ini."</td><td>".$fim."</td><td>".MinToHrMin(datediff("n", $ini, $fim))."</td></tr>";
                        }
                        $andando = $andando + datediff("n", $ini, $fim);
                        $aa = $aa + datediff("n", $ini, $fim);
                        if(diadasemana($fim) != "1" && diadasemana($fim) != "7")
                            {$saa = $saa + datediff("n", $ini, $fim);}else{$fsaa = $fsaa + datediff("n", $ini, $fim);}
                        $ini = $fim;
                        $st = 2;
                    }
                    if($st == 2){
                        $fim = date("d/m/Y H:i", strtotime($RS["dt"]));
                        $st = 2;
                    }
                }
            }

            $dia    = date("d", strtotime($RS["dt"]));
            $ultimo = date("d/m/Y", strtotime($RS["dt"]));
        }
    }

    if($st == 1){
        $html .= "<tr bgcolor='".$cor."'><td>Andando</td><td>".$ini."</td><td>".$fim."</td><td>".MinToHrMin(datediff("n", $ini, $ultimo." 23:59"))."</td></tr>";
        $andando = $andando + datediff("n", $ini, $ultimo." 23:59");
        $aa = $aa + datediff("n", $ini, $ultimo." 23:59");
        if(diadasemana($ultimo) != "1" && diadasemana($ultimo) != "7")
            {$saa = $saa + datediff("n", $ini, $ultimo." 23:59");}else{$fsaa = $fsaa + datediff("n", $ini, $ultimo." 23:59");}
    }

    if($st == 2){
        $html .= "<tr bgcolor='".$cor."'><td>Parado</td><td>".$ini."</td><td>".$fim."</td><td>".MinToHrMin(datediff("n", $ini, $ultimo." 23:59"))."</td></tr>";
        //$teste = number_format((1439/60),1)-substr(number_format((1439/60),1),-2);
        //if($teste == 24){$teste = 23;}
        //echo $teste;
        //echo /*MinToHrMin(*/datediff("n", $ini, $ultimo." 23:59")/*)*/;
        $parado = $parado + datediff("n", $ini, $ultimo." 23:59");
        $pp = $pp + datediff("n", $ini, $ultimo." 23:59");
        if(diadasemana($ultimo) != "1" && diadasemana($ultimo) != "7")
            {$spp = $spp + datediff("n", $ini, $ultimo." 23:59");}else{$fspp = $fspp + datediff("n", $ini, $ultimo." 23:59");}
    }

    $html .= "<tr bgcolor='".$cor."'><td colspan='3'><b>Andando no Dia</b></td><td><b>".MinToHrMin($andando)."</b></td></tr>";
    $html .= "<tr bgcolor='".$cor."'><td colspan='3'><b>Parado no Dia</b></td><td><b>".MinToHrMin($parado)."</b></td></tr>";
    $html .= "<tr bgcolor='#ffff99'><td colspan='3'><b>Total Andando no Período Semana</b></td><td><b>".MinToHrMin($saa)."</b></td></tr>";
    $html .= "<tr bgcolor='#ffff99'><td colspan='3'><b>Total Parado no Período Semana</b></td><td><b>".MinToHrMin($spp)."</b></td></tr>";
    $html .= "<tr bgcolor='#33ff66'><td colspan='3'><b>Total Andando no Período Final de Semana</b></td><td><b>".MinToHrMin($fsaa)."</b></td></tr>";
    $html .= "<tr bgcolor='#33ff66'><td colspan='3'><b>Total Parado no Período Final de Semana</b></td><td><b>".MinToHrMin($fspp)."</b></td></tr>";
    $html .= "<tr bgcolor='#FFDFB0'><td colspan='3'><b>Total Andando no Período</b></td><td><b>".MinToHrMin($aa)."</b></td></tr>";
    $html .= "<tr bgcolor='#FFDFB0'><td colspan='3'><b>Total Parado no Período</b></td><td><b>".MinToHrMin($pp)."</b></td></tr>";
    $html .= "<tr><td colspan='4' style='color:red' align='center'><b>* Entende-se por ANDANDO: Deslocamento Maior 300 metros</b></td></tr>";

$html .= "</table>";
}
    
asked by anonymous 25.11.2017 / 20:40

0 answers