Calculate output per hour using HTML5 and PHP

Asked

Viewed 140 times

1

Good,

I’m new here and also in the programming language :)

I’m developing a Production per hour calculator in PHP. I was part of the code, I can feed the input and do the calculation, but the result does not come out in HOURS format (00:00:00), I could not convert the value, I want to be filled in the ":" automatically.

I need to add the time between two times.

Example: Start: 07:00:00 - End: 12:00:00 - Total: 05:00:00.

Thanks in advance.

Below is my code. But here is not coming out the result.

<!DOCTYPE html>
<html lang="pt-br">
<head>
	<meta charset="UTF-8"/>
	<link rel="stylesheet" href="_css/estilo.css"/>
	<title>Controle de produção</title>
</head>
<body>
   <div>
       <form method="post" action="index.php">
           <class>Início</class> <class>Fim</class> <class>Horas Trab.</class><br/>
           <input type="datatime" placeholder="inicio 1" name="i1"/>
           <input type="datatime" placeholder="fim 1" name="f1"/><br/>
           <input type="datatime" placeholder="inicio 2" name="i2"/>
           <input type="datatime" placeholder="fim 2" name="f2"/><br/>
           <input type="submit" value="Calcular produção"/>
       </form>
       	<?php
    	   $inicio1 = isset($_POST["i1"])?$_POST["i1"]:'00:00:00';
    	   $inicio2 = isset($_POST["i2"])?$_POST["i2"]:0;
    	   $fim1 = isset($_POST["f1"])?$_POST["f1"]:0;
    	   $fim2 = isset($_POST["f2"])?$_POST["f2"]:0;
    	   $rht1 = ($fim1 - $inicio1);
    	   $rht2 = ($fim2 - $inicio2);
    	   
    	   echo "$rht1 e $rht2";
    	   
    	?>
    </div>
</body>
</html>
 

  • You need to use date take a look at the documentation... https://www.php.net/manual/en/function.date.php

  • As I said, I’m learning programming, and I still can’t read the documentation, :), With the one who sent me I started to understand how the reading works, but if you have any tips, I will be grateful. I thank you for your attention and help ;), and also thank you to all who answered me.

2 answers

2

Opa, as Lodi commented, it would be nice to take a look at the PHP documentation.

This example here is using the class Datetime

$inicio1 = new DateTime('10:00:00');
$fim1 = new DateTime('12:30:30');

$diff = $inicio1->diff($fim1);

$diff_horas = $diff->format('%h:%i:%s');

There’s enough to do with this class.

I hope it helps you. :)

1

Do not use type="datetime" which is obsolete (Chrome itself is not supports more). Use type="datetime-local".

Use the class DateTime() in the values sent and take the difference with diff(). In the case below I took the hours and minutes separately and then formatted with date().

<div>
   <form method="post" action="teste2.php">
      <class>Início</class> <class>Fim</class> <class>Horas Trab.</class><br/>
      <input type="datetime-local" placeholder="inicio 1" name="i1"/>
      <input type="datetime-local" placeholder="fim 1" name="f1"/><br/>
      <input type="datetime-local" placeholder="inicio 2" name="i2"/>
      <input type="datetime-local" placeholder="fim 2" name="f2"/><br/>
      <input type="submit" value="Calcular produção"/>
   </form>
   <?php
   $inicio1 = isset($_POST["i1"]) ? new DateTime($_POST['i1']) : 0;
   $inicio2 = isset($_POST["i2"]) ? new DateTime($_POST['i2']) : 0;
   $fim1 =    isset($_POST["f1"]) ? new DateTime($_POST['f1']) : 0;
   $fim2 =    isset($_POST["f2"]) ? new DateTime($_POST['f2']) : 0;
   $dif1 = $fim1->diff($inicio1);
   $dif2 = $fim2->diff($inicio2);

   // formata em horas (formato 24h) : minutos (00 a 59) : 00
   $rht1 = date("H:i:00", strtotime("$dif1->h:$dif1->i"));
   $rht2 = date("H:i:00", strtotime("$dif2->h:$dif2->i"));

   echo "$rht1 e $rht2";

   ?>
</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.