0

I need to calculate the hours that are between two hours:

for example I have a schedule from 18:00 to 02:00; I need to know how many of those hours are within 22:00 to 06:00.

for this case it would be 4 hours;

how can I do that in PHP?

  • I don't need to know the time difference from 22 to 06

what I need to know is the difference in hours from 22 to 02. since this time frame is the one within the specified parameters

  • I clarify that I also need to know the minutes

this is the code i have, but it seems very complex and it doesn't always work

            $he = new datetime('2020-01-01 '.$horaInicio);
        $hs = new datetime($dateFinal.' '.$horaFinal);

        $inicio_noche = new datetime($he->format('y-m-d').' 22:00:00');
        $fin_noche = new datetime($hs->format('y-m-d').' 06:00:00');
        $medianoche = new datetime($hs->format('y-m-d').' 00:00:00');

        if ($he->format('d') == $hs->format('d')) {
            if ($he->format('H:i:s') >= $medianoche->format('H:i:s') && $hs->format('H:i:s') <= $fin_noche->format('H:i:s')) {
                $diff = $he->diff($hs);
                $horas['nocturnas'] = new datetime('2020-01-01 '.$diff->format('%H:%i:%s'));
            }
            if ($he->format('H:i:s') < $fin_noche->format('H:i:s') && $hs->format('H:i:s') > $fin_noche->format('H:i:s')) {
                $diff = $he->diff($fin_noche);
                $horas['nocturnas'] = new datetime('2020-01-01 '.$diff->format('%H:%i:%s'));
            }
            if ($he->format('H:i:s') > $fin_noche->format('H:i:s') && $hs->format('H:i:s') > $fin_noche->format('H:i:s')) {
                $diff = $he->diff($fin_noche);
                $horas['nocturnas'] = new datetime('2020-01-01 '.$diff->format('%H:%i:%s'));
            }
        } else {
            if ($he->format('H:i:s') >= $inicio_noche->format('H:i:s') && $hs->format('H:i:s') <= $fin_noche->format('H:i:s')) {
                $diff = $he->diff($hs);
                $horas['nocturnas'] = new datetime('2020-01-01 '.$diff->format('%H:%i:%s'));
            }
            if ($he->format('H:i:s') < $inicio_noche->format('H:i:s') && $hs->format('H:i:s') <= $fin_noche->format('H:i:s')) {
                $diff = $inicio_noche->diff($hs);
                $horas['nocturnas'] = new datetime('2020-01-01 '.$diff->format('%H:%i:%s'));
            }
            if ($he->format('H:i:s') < $inicio_noche->format('H:i:s') && $hs->format('H:i:s') > $fin_noche->format('H:i:s')) {
                $diff = $inicio_noche->diff($fin_noche);
                $horas['nocturnas'] = new datetime('2020-01-01 '.$diff->format('%H:%i:%s'));
            }
        }
SM23
  • 1
  • 2

2 Answers2

2
$start_time = strtotime('06:00');
$end_time = strtotime('22:00');
$diff = abs($end_time)/3600;
echo $diff;
Dharman
  • 30,962
  • 25
  • 85
  • 135
The IRF
  • 470
  • 3
  • 13
  • thank you, but this code only returns the difference between 10pm and 6am I need to know the total number of hours between two hours, only if they are between 22:00 to 06:00 – SM23 Oct 24 '20 at 16:04
0

You should try date_diff.

$time1 = '2020-10-24 18:00:00';
$time2 = '2020-10-24 19:00:00';
$difference = date_diff(date_create($time1),date_create($time2))->format('%h');

Output: 1

With the above you can change what format you want the difference.

%y years
%m months
%d days
%h hours
%i minutes
%s seconds

Edit: This didn't adds the values. So you need to make it manually like this:

$time1 = '2020-10-22 22:00:00';
$time2 = '2020-10-24 06:00:00';
$diff = date_diff(date_create($time1),date_create($time2));
$diff_ho = $diff->format('%y')*(24*365)+$diff->format('%m')*(24*30)+$diff->format('%d')*24+$diff->format('%h');
CleverSkull
  • 479
  • 3
  • 10
  • I need to know the total number of hours between two hours, only if they are between 22:00 to 06:00 In the case that you put the output it will be 0 – SM23 Oct 24 '20 at 16:07
  • You can check this: https://stackoverflow.com/questions/3108591/calculate-number-of-hours-between-2-dates-in-php – CleverSkull Oct 24 '20 at 16:15