Obtain total hours from a range of dates that meet time intervals in php

0

How can it be done in PHP to get the number of hours from a range of dates that meet a time interval? For example, having the range of dates:

SUBIDA = 2017-01-02 01:00 - BAJADA = 2017-01-02 22:00

And knowing that the intervals are:

DE 00:00 a 06:00 y DE 22:00 a 23:59

The total hours of the range that apply between these intervals must be found. For the case of the example, with the provided data should be obtained:

DE 00:00 a 06:00 -> 5 horas
DE 22:00 a 23:59 -> 2 horas

I'm using this code for the moment:

if ($h1<$h2)
                 {
                     $res2=MIN($h2,'23:59:59')-MAX($h1,'22:00:00');
                     $res1=MIN($h2,'06:00:00')-MAX($h1,'00:00:00');
                 }
                 else
                 {
                     $res2=MAX(0,('23:59:59')-($h1))+MAX(0,$h2-('22:00:00'));
                     $res1=MAX(0,('06:00:00')-($h1))+MAX(0,$h2-'00:000:00');
                 }

Where, $ h1 = upload in 'H: i: s' format and $ h2 = download in 'H: i: s' format.

The code works for me but not very precise, since it sometimes returns me 1 or 2 hours less in some interval.

Does anyone have a better way to do it? Thanks in advance.

Reginaldo Bray

    
asked by Reginaldo Bray 16.03.2018 в 13:41
source

1 answer

0

After so much testing, I have come to the following that meets what I needed:

$hora_inicio = $ini->format("H:i");
                  $hora_fin = $fin->format("H:i");
                  //INTERVALO DE 00:00 a 06:00
                  if(($hora_inicio<='06:00') && ($hora_fin>'06:00') )
                  {

                      $intervalo1 = clone $ini;
                      $intervalo1 = $intervalo1->setTime(06,00);
                      $diferencia1 = ($intervalo1->diff($ini));
                      $diferencia1 = ($diferencia1->format('%d') * 24) + $diferencia1->format('%H') + ($diferencia1->format('%i') / 60);

                  }
                  else if(($hora_inicio<='06:00') && ($hora_fin<='06:00') )
                  {   $diferencia1 = ($fin->diff($ini));
                      $diferencia1 = ($diferencia1->format('%d') * 24) + $diferencia1->format('%H') + ($diferencia1->format('%i') / 60);
                  }
                  else if ($hora_inicio>'06:00')
                  {
                      $diferencia1 = 0;
                  }

                  //INTERVALO 22:01 a 23:59

                  if (($hora_inicio>=$horares.':01') && ($hora_fin<='23:59') )
                  {

                      $diferencia2 = ($ini->diff($fin));
                      $diferencia2 = ($diferencia2->format('%d') * 24) + $diferencia2->format('%H') + ($diferencia2->format('%i') / 60);
                  }
                  else if (($hora_inicio<$horares.':01') && ($hora_fin>=$horares.':01'))
                  {

                      $intervalo2 = clone $ini;
                      $intervalo2 = $intervalo2->setTime($horares,01);
                      $diferencia2 = ($intervalo2->diff($fin));
                      $diferencia2 = ($diferencia2->format('%d') * 24) + $diferencia2->format('%H') + ($diferencia2->format('%i') / 60);

                  }
                  else if ($hora_fin<$horares.':01')
                  {

                      $diferencia2=0;
                  }

                   return ($diferencia1+$diferencia2);

I did several random tests and it worked for me. The only requirement is that the start date and the final date (Y-m-d) must be the same which, for what I need it, is fine. If someone can improve it, they tell me.

    
answered by 20.03.2018 / 18:20
source