Calculate PHP hour percentage

Asked

Viewed 501 times

-1

I need to calculate the percentage of each hour of one array. For example:

Hora1: 4:45 am
Time 2: 1:30 am
Time 3: 00:45
Total: 07:00

$array = array('04:45','01:30','00:45');

I did the calculation in excel, and the result was:

inserir a descrição da imagem aqui

EDIT

Follow the PHP I’m trying, but it’s returning different values:

$array = array('04:45', '01:30', '00:45');
      foreach ($array as $tem => $key) {
      $total = '07:00';
      $tempo = $key;

      $percentual = round(($tempo / $total) * 100);

      $percentual_total .= "Tempo: " . $tempo . ", Percentual: " . $percentual . "<br>";
}

 echo $percentual_total;

Return of PHP

inserir a descrição da imagem aqui

  • percentage of what? no one guesses here, and if Voce did in excel, it differs little from doing in php or any other language, the process is pretty much the same.

  • Of each hour of the array, you did not see in the question title?

  • I saw @Eduardo Santos, but, see, 67,857... and the 5-hour percentage? 1 hour? , it was kind of groundless your question, and it was like I said, from excel pro php eh a hop of nothing, if Voce Jah has there, Voce could tell us what would be your difficulty in php?

  • Not that that’s the answer to your question, but I think if you just want the percentage, it would be easier to convert everything to minutes

  • I’ll edit the question with what I’m trying to do.

  • It gave to understand the purpose. The question that was badly formulated. :)

Show 1 more comment

2 answers

2

One solution would be this:

// Array com os horários
$array = array('04:45', '01:30', '00:45');

// Array que será armazenado as porcentagens
$porc = array();

// Total em mintuos
$total = 0;

// Varre o array dos horários e converte para inteiro
foreach ($array as $key => $time) {
    sscanf($time, "%d:%d", $hours, $minutes);
    $mins = $hours * 60 + $minutes;
    array_push($porc, $mins);
    $total += $mins;
}

// Converte os inteiros para porcentagem
$porc = array_map(function($value) use ($total) {
    return round(($value / $total) * 100, 2);
}, $porc);

// Exibe os valores
foreach ($array as $key => $time) {
    echo "Tempo: " . $time . ", Percentual: " . $porc[$key] . "%<br>";
}
  • Thank you for the reply friend, the code met what I needed. Hug.

2


result - ideone

I transformed $total in minutes and in the foreach at each iteration, the value of the element $key also in minutes to perform the calculations with the same units.

$array = array('04:45', '01:30', '00:45');

$total = '07:00';
$explodeHoraTotal = explode(":",$total); //retorna um array onde cada elemento é separado por ":"
$minutosTotal = $explodeHoraTotal[0];
$minutosTotal = $minutosTotal*60;
$total =$minutosTotal+$explodeHoraTotal[1];

foreach ($array as $tem => $key) {

      $quebraHora = explode(":",$key); //retorna um array onde cada elemento é separado por ":"
      $minutos = $quebraHora[0];
      $minutos = $minutos*60;
      $tot =$minutos+$quebraHora[1];

      $percentual = round(($tot / $total) * 100);

      $percentual_total .= "Tempo: " . $key . ", Percentual: " . $percentual . "<br>";
}

 echo $percentual_total;


Because your script went wrong?

We should always keep in mind that we can only perform mathematical operations for the same quantity with numbers representing exactly the same unit of measure. You were looking for a mathematical operation involving numbers with two units of measurement, hour and minutes. It could, if it turned into decimal hours, example, (4,5hs/1,5hs) outworking: 3 períodos de 1,5hs

In the code below I transformed the array into decimal hours $array = array('4.75', '1.5', '0.75'); and the total hours as well $total = '7.0';

  • decimal hours - example: 4 hours and 45 minutes, in decimals, are 4 hours and 3 quarters of an hour, ie 4.75

That way we can do mathematical operations as can be seen below:

$array = array('4.75', '1.5', '0.75');

$total = '7.0';

 foreach ($array as $tem => $key) {

      $percentual = round(($key / $total) * 100);

      $percentual_total .= "Tempo: " . $key . ", Percentual: " . $percentual . "\n";
}

 echo $percentual_total;

ideone



Going back to the operations you were wanting to do, we have that PHP simply ignored the hours after the two points including the two points. So 04:45 turned into 4 and '07:00' in 7. Upshot round((4 / 7) * 100); is exactly 57 see in ideone

  • This solution changes the assumptions of the doubt. The entries must be in HH:MM hour format and the output must be the percentage calculation.

  • @Diegomarques I don’t think you understand the premise of the supplementary explanation that looks at the fact that the author is doing a mathematical operation with units of measures not appropriate. The solution is stamped on the first part of the answer and perfectly meets the author’s question.

  • Thanks for the explanation friend, the code met perfectly. Hug.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.