PHP minute counter

1

PHP timer
How to measure the time elapsed since you pressed a button on a form     until he pressed another

<?php

session_start();

$inicio=date('h i s');
$_SESSION["inicio"] = $inicio;
echo  $_SESSION["inicio"];

$fin=date('h i s');
$_SESSION["fin"] = $fin;
echo  $_SESSION["fin"];

$dif=$fin - $inicio;
echo $dif;

?>
<forn action="" method="POST">
<input type="submit" name="inicio" value="inicio">
<input type="submit" name="fin" value="fin">
</forn>
</body>
</html>
    
asked by Marta 09.08.2017 в 19:43
source

2 answers

1

I know it's school work ... but I was bitten by the language that some people say could not be done without using AJAX ...

The existing test that can be done WITHOUT AJAX: p

The logic is commented on in the code ....

<?php


session_start();

// Primero controlamos si el método del envío es POST
if ( $_SERVER['REQUEST_METHOD'] === 'POST') {       

    // Chequeamos si el botón inicio se ha presionado
    if ( isset( $_POST['inicio'] ) && $_POST['inicio'] == 'inicio' ) {

        // Creamos una variable cual me indica que era el primer intento
        $_SESSION['primer_intento'] = true;

        $inicio = date('h:i:s');

        $_SESSION["inicio"] = $inicio;

        echo  $_SESSION["inicio"];      

    }   

    // Aquí chequeamos si la variable del primer_intento está puesta y si es true
    // y también chequeamos si el botón "fin" se ha presionado o no
    if ( isset( $_SESSION['primer_intento'] ) && $_SESSION['primer_intento'] === true && isset( $_POST['fin'] ) && $_POST['fin'] == 'fin' ) {       


        $fin = date('h:i:s');

        $_SESSION["fin"] = $fin;

        // Y por último calculamos la diferencia en minutos
        $inicio = strtotime($_SESSION["inicio"]);
        $fin = strtotime($_SESSION["fin"]);
        echo round(abs($fin - $inicio) / 60, 2). " minutos";

        // Ponemos false para que vuelva a presionar el botón de "inicio"
        $_SESSION['primer_intento'] = false;
    }
}

?>
<!-- Es <form> no <forn> -->
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    <input type="submit" name="inicio" value="inicio">
    <input type="submit" name="fin" value="fin">
</form>
    
answered by 09.08.2017 / 20:44
source
-1

This should serve you, you can not do it with PHP only, here is an example with AJAX.

//EN HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<form>
  <input type="button" id="inicio" class="myBtn" value="inicio">
  <input type="button" id="fin" class="myBtn" value="fin">
</form>

<script>
  $(function(){

    $(document).on('click','.myBtn',function(){//DETECTAR CUANDO SE DE CLICK
      if($(this).attr('id') == 'inicio')//IDENTIFICAR A QUE SE LE DIO CLICK Y SACAR SU ID Y METERLO AL VALOR QUE SE ENVIARA
        var valor = 'inicio';
      else
        var valor = 'fin';


        //CONEXION CON AJAX
        $.ajax({
          url:'dameTiempo.php',//LE DAMOS UNA RUTA DONDE ESTA EL ARCHIVO QUE PROCESARA LA PETICION
          method:'POST',
          data:valor:valor,//LE ENVIAMOS LOS VALORES
          success:function(resp)
          {
              alert(resp);//LO QUE NOS RESPONDA EL PHP LO METEMOS EN UN ALERT
          }
        });
    });
  });
</script>


//EN PHP
<?php
session_start();
$_SESSION['inicio'] = 0; //INICIAMOS LA VARIABLE EN 0
$_SESSION['fin'] = 0;


$valor = $_REQUEST['valor']; //RECUPERAMOS EL VALOR QUE LLEGO POR AJAX


if($valor == 'inicio')//DEPENDIENDO DEL VALOR QUE LLEGO SE LO ASIGNO A MI VARIABLE DE SESION
  $_SESSION['inicio'] = $valor;
else
  $_SESSION['fin'] = $valor;

$newTime = ($_SESSION['inicio'] - $_SESSION["fin"])/60; //HACEMOS EL CALCULO
echo round(intval($newTime));//REGRESAMOS EL VALOR REDONDEADO Y EN ENTEROS PARA QUE SEAN MINUTOS


?>
    
answered by 09.08.2017 в 20:20