Converting total hours of a month into days and hours

Asked

Viewed 42 times

-1

Hello!

I am working on a data_set in which the time measure is in Total Hours Races of the month. Going from hour 1 to hour 734. I want to convert this measure of time into hours of the month per day.

For example:

  • Input:

step = [1, 2, 3, 22, 23, 24, 25, 26, 27, 46, 47, 48, 49, 50]

  • Outputs:

hour = [1, 2, 3, 22, 23, 0, 1, 2, 3, 22, 23, 0, 1, 2]

for s in step:
i = 1
upper_range_hour = i*24 
lower_range_hour = upper_range_hour-24 
if (s >= lower_range_hour) & (s < (upper_range_hour)): 
    if s < 24: 
        hour.append(s)
    else:
        hour.append(s%lower_range_hour)
else:
    hour.append(0)
    i += 1

But the return I have is the following for this example: [1, 2, 3, 22, 23, 0, 0, 0, 0, 0, 0, 0]

Can anyone help me? I think the problem is perhaps the sum of "counted i" OR "hour.append(s%lower_range_hour)"

Thank you!

  • "where the time measure is in hours of the month" and then "I want to convert that time measure into hours of the month". I think it’s a little controversial. You can edit the question? :)

1 answer

-3

Hello! There is probably a simpler solution but I leave it to you to think or solve your problem temporarily:

$hora = 0;
$dia = 1;
$horas = 734; // horas pre definidas
// cria um array associativo onde cada hora é um novo array que possui valores como hora e dia
for($i = 0; $i < $horas; $i++)
{

    if($hora != 24){
        $arr[$i] = $time=['hora' => $hora,'dia' => $dia];
    } else {
        $hora = 0;
        $dia++;
        $arr[$i] = $time=['hora' => $hora,'dia' => $dia];
    }
    $hora++;
}
// seu input
$step = [1, 2, 3, 22, 23, 24, 25, 26, 27, 46, 47, 48, 49, 50];

// para cada input ele separa por associação em um novo array
foreach($step as $item){
        $newarr[] = $arr[$item];
    }
    
// array_column separa a coluna hora e dias do array em um novo array
$horas_finais = array_column($newarr,'hora');
$dias_finais = array_column($newarr,'dia'); 

//print...
echo "horas: ";
print_r($horas_finais);
echo "<br>";
echo "dias: ";
print_r($dias_finais);

//saidas
// horas: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 22 [4] => 23 [5] => 0 [6] => 1 [7] => 2 [8] => 3 [9] => 22 [10] => 23 [11] => 0 [12] => 1 [13] => 2 )
// dias: Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 1 [4] => 1 [5] => 2 [6] => 2 [7] => 2 [8] => 2 [9] => 2 [10] => 2 [11] => 3 [12] => 3 [13] => 3 ) 

Obs: I made in PHP that is my language, but I think I can easily adapt to the language you are used to.

Browser other questions tagged

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