SM23
SM23

Reputation: 1

calculate hours only if they are between two hours PHP

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?

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

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'));
            }
        }

Upvotes: 0

Views: 56

Answers (2)

CleverSkull
CleverSkull

Reputation: 505

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');

Upvotes: 0

The IRF
The IRF

Reputation: 470

$start_time = strtotime('06:00');
$end_time = strtotime('22:00');
$diff = abs($end_time)/3600;
echo $diff;

Upvotes: 2

Related Questions