1

My Code is :

$hm = strtotime('33:00:00')-strtotime('00:30:00');


$hms = gmdate("H:i:s", $hm);

i need the output is 32:30:00

apokryfos
  • 38,771
  • 9
  • 70
  • 114
  • Unfortunately from what I can see when dealing with durations above 24 hours you'll need to manually caclulate total hours and remainder minutes `gmdate` will not work for you. So the duped answer is probably your best bet – apokryfos Nov 01 '22 at 11:25
  • The accepted answer from https://stackoverflow.com/questions/29664870/calculate-difference-in-hours-between-two-times-in-php returns an incorrect result for this question. https://3v4l.org/Qhd81 – jspit Nov 01 '22 at 12:53

2 Answers2

2

Use this code to get subtracted values.

$time1 = '33:00:00';
$time2 = '00:00:00';

echo 'Subtract = '.subtractTwoTimes($time1, $time2);

function subtractTwoTimes($time1, $time2){
  $time1_parts = explode(':', $time1);
  $time1_hours = $time1_parts[0];
  $time1_mins = $time1_parts[1];
  $time1_secs = $time1_parts[2];


  $time2_parts = explode(':', $time2);
  $time2_hours = $time2_parts[0];
  $time2_mins = $time2_parts[1];
  $time2_secs = $time2_parts[2];

  $total_hours = $time1_hours - $time2_hours;
  $total_mins = $time1_mins - $time2_mins;
  $total_secs = $time1_secs - $time2_secs;

  if($total_secs < 0){
      $total_secs += 60;
      $total_mins--;
  }

  if($total_mins < 0){
      $total_mins += 60;
      $total_hours --;
  }

  return $total_hours.':'.$total_mins.':'.$total_secs;
}
0

To get added or subtracted value from same function. This is an edited answer. I optimized the code to make it work for you in one function.

$time1 = '33:00:00';
$time2 = '00:30:00';

echo 'Add = '. addOrSubtractTwoTimes($time1, $time2, 'add');
echo '<br>';
echo 'Subtract = '. addOrSubtractTwoTimes($time1, $time2, 'subtract');

function addOrSubtractTwoTimes($time1, $time2, $operation){
  $time1_parts = explode(':', $time1);
  $time1_hours = $time1_parts[0];
  $time1_mins = $time1_parts[1];
  $time1_secs = $time1_parts[2];

  $time2_parts = explode(':', $time2);
  $time2_hours = $time2_parts[0];
  $time2_mins = $time2_parts[1];
  $time2_secs = $time2_parts[2];

  switch ($operation){
    case 'add':
        $total_hours = $time1_hours + $time2_hours;
        $total_mins = $time1_mins + $time2_mins;
        $total_secs = $time1_secs + $time2_secs;
        if($total_secs > 59){
            $extra_mins = floor($total_secs/60);
            $total_secs = $total_secs%60;

            $total_mins += $extra_mins;
        }

        if($total_mins > 59){
            $extra_hours = floor($total_mins/60);
            $total_mins = $total_mins%60;

            $total_hours += $extra_hours;
        }
        break;
        
    case 'subtract':
        $total_hours = $time1_hours - $time2_hours;
        $total_mins = $time1_mins - $time2_mins;
        $total_secs = $time1_secs - $time2_secs;
        if($total_secs < 0){
            $total_secs += 60;
            $total_mins--;
        }

        if($total_mins < 0){
            $total_mins += 60;
            $total_hours --;
        }
        break;
  }
  
  return sprintf('%02d', $total_hours).':'.sprintf('%02d', $total_mins).':'.sprintf('%02d', $total_secs);

}