Calculating hours

2

I need to calculate the exact time that a certain record has to be analyzed.

Each record has a range where it can be parsed, and a maximum value in hours for term p>

Example

Endtime=Dateregistration+Term=12/26/201404:24:31

ButIneedthetimeoutofrangetobeignored,ie:

  • Day25wouldbecountedonly01:35:29becausetherangeisuntil10:00pm
  • Wouldcountbackfrom08h,finishingtheexacttimeas12/26/201414:24:31

Mycurrentsolutionisloopforeachrecord:

$incremento=0$cadastro="26/12/2014 04:24:31"
$intervalo_inicial = 8
$intervalo_final = 10
$prazo = 8

while ($incremento <> $prazo) {
    if ($cadastro >= $intervalo_inicial) and ($cadastro <= $intervalo_final) {
        $incremento += 1
    }
    // adiciona 1 hora
    $cadastro = date($cadastro, strtotime('1 hour'));
}

However, calculating 100,000 records makes the task very slow. Any type of solution is valid, database, formula, logic ..

    
asked by anonymous 29.12.2014 / 03:33

1 answer

1

You can use split to know how many whole days you've used and how many hours left.

To avoid confusion in this example (due to two different values being 8), I changed the deadline to 7 hours. Also, in the real version I would use the minimum time counting unit to do the counts (seconds or milliseconds, it depends on what you are using) but in the example I represented everything using hours to get clearer.

$tempo_inicio = 20:24:31 - 08:00:00  // = 12:24:31
$prazo = 07:00:00
$tempo_fim = $hora_inicio + $prazo // = 19:24:31

$horas_por_dia = 22:00:00 - 08:00:00 // = 14:00:00

// quociente e resto da divisão
$dias_gastos = $tempo_fim / $horas_por_dia  // 1
$horas_ultimo_dia = $tempo_fim % $horas_por_dia // 5:24:31

$dia_fim = $dia_inicio + $dias_gastos // dia 26
$hora_fim = 8:00:00 + $horas_ultimo_dia // 13:24:31
    
29.12.2014 / 05:04