Time Manipulation [HH: MM: SS] - php and mysql

0

Good afternoon, guys.

I need to manipulate time fields [HH: MM: SS] in PHP itself.

I get the values in text in the HTML form as in the following example:

 <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12">P.E: <span data-toggle="tooltip" title=Horário de entrada" class="required">*</span></label>
                        <div class="col-md-9 col-sm-9 col-xs-12">
                          <input type="text" name="hora_entrada" class="form-control" data-inputmask="'mask' : '99:99:99'" placeholder="HH:MM:SS" required="">
                        </div>
                      </div>

  <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12">S.B.: <span data-toggle="tooltip" title="Horário de saída class="required">*</span></label>
                        <div class="col-md-9 col-sm-9 col-xs-12">
                          <input type="text" name="hora_saida" class="form-control" data-inputmask="'mask' : '99:99:99'" placeholder="HH:MM:SS" required="">
                        </div>
                      </div>

I bring the PHP script to do the calculation:

$saida = $_POST['saida'];
$entrada = $_POST['entrada'];

//cálculo do hora trabalhada

	 		$ps = strtotime($saida);
	 		$pe = strtotime($entrada);

	 		$horas = ($ps-$pe);

So if I put 14:20:00 - 06:00:00, the result should be 08:20:00

But it's coming: 00:08:00

Does anyone know how I can do these calculations?

Thank you

    
asked by anonymous 01.03.2018 / 20:34

1 answer

2

Your calculation is correct, you just need to hit the view, which can be done as follows:

$ps = "14:20:00";
$pe = "06:00:00";

$ts1 = strtotime( $ps);
$ts2 = strtotime($pe);
$diff = abs($ts1 - $ts2); // diferença entre os dois tempos

echo gmdate("H:i:s", $diff); // transformação do valor numérico $diff em hora string novamente
    
01.03.2018 / 20:54