skip to Main Content

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

2

Answers


  1. 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;
    }
    
    Login or Signup to reply.
  2. 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);
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search